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