1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.jdbc; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.transaction.TransactionException; |
15 | |
import org.mule.config.i18n.CoreMessages; |
16 | |
import org.mule.transaction.AbstractSingleResourceTransaction; |
17 | |
import org.mule.transaction.IllegalTransactionStateException; |
18 | |
import org.mule.transaction.TransactionRollbackException; |
19 | |
import org.mule.transport.jdbc.i18n.JdbcMessages; |
20 | |
|
21 | |
import java.sql.Connection; |
22 | |
import java.sql.SQLException; |
23 | |
|
24 | |
import javax.sql.DataSource; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public class JdbcTransaction extends AbstractSingleResourceTransaction |
30 | |
{ |
31 | |
|
32 | |
public JdbcTransaction(MuleContext muleContext) |
33 | |
{ |
34 | 0 | super(muleContext); |
35 | 0 | } |
36 | |
|
37 | |
public void bindResource(Object key, Object resource) throws TransactionException |
38 | |
{ |
39 | 0 | if (!(key instanceof DataSource) || !(resource instanceof Connection)) |
40 | |
{ |
41 | 0 | throw new IllegalTransactionStateException( |
42 | |
CoreMessages.transactionCanOnlyBindToResources("javax.sql.DataSource/java.sql.Connection")); |
43 | |
} |
44 | 0 | Connection con = (Connection)resource; |
45 | |
try |
46 | |
{ |
47 | 0 | if (con.getAutoCommit()) |
48 | |
{ |
49 | 0 | con.setAutoCommit(false); |
50 | |
} |
51 | |
} |
52 | 0 | catch (SQLException e) |
53 | |
{ |
54 | 0 | throw new TransactionException(JdbcMessages.transactionSetAutoCommitFailed(), e); |
55 | 0 | } |
56 | 0 | super.bindResource(key, resource); |
57 | 0 | } |
58 | |
|
59 | |
protected void doBegin() throws TransactionException |
60 | |
{ |
61 | |
|
62 | 0 | } |
63 | |
|
64 | |
protected void doCommit() throws TransactionException |
65 | |
{ |
66 | 0 | if (resource == null) |
67 | |
{ |
68 | 0 | logger.warn(CoreMessages.commitTxButNoResource(this)); |
69 | 0 | return; |
70 | |
} |
71 | |
|
72 | |
try |
73 | |
{ |
74 | 0 | ((Connection)resource).commit(); |
75 | 0 | ((Connection)resource).close(); |
76 | |
} |
77 | 0 | catch (SQLException e) |
78 | |
{ |
79 | 0 | throw new TransactionException(CoreMessages.transactionCommitFailed(), e); |
80 | 0 | } |
81 | 0 | } |
82 | |
|
83 | |
protected void doRollback() throws TransactionException |
84 | |
{ |
85 | 0 | if (resource == null) |
86 | |
{ |
87 | 0 | logger.warn(CoreMessages.rollbackTxButNoResource(this)); |
88 | 0 | return; |
89 | |
} |
90 | |
|
91 | |
try |
92 | |
{ |
93 | 0 | ((Connection)resource).rollback(); |
94 | 0 | ((Connection)resource).close(); |
95 | |
} |
96 | 0 | catch (SQLException e) |
97 | |
{ |
98 | 0 | throw new TransactionRollbackException(CoreMessages.transactionRollbackFailed(), e); |
99 | 0 | } |
100 | 0 | } |
101 | |
} |