1 10 11 package org.mule.providers.jdbc; 12 13 import org.mule.config.i18n.Message; 14 import org.mule.config.i18n.Messages; 15 import org.mule.transaction.AbstractSingleResourceTransaction; 16 import org.mule.transaction.IllegalTransactionStateException; 17 import org.mule.transaction.TransactionRollbackException; 18 import org.mule.umo.TransactionException; 19 20 import javax.sql.DataSource ; 21 22 import java.sql.Connection ; 23 import java.sql.SQLException ; 24 25 28 public class JdbcTransaction extends AbstractSingleResourceTransaction 29 { 30 31 public JdbcTransaction() 32 { 33 super(); 34 } 35 36 42 public void bindResource(Object key, Object resource) throws TransactionException 43 { 44 if (!(key instanceof DataSource ) || !(resource instanceof Connection )) 45 { 46 throw new IllegalTransactionStateException(new Message( 47 Messages.TX_CAN_ONLY_BIND_TO_X_TYPE_RESOURCES, "javax.sql.DataSource/java.sql.Connection")); 48 } 49 Connection con = (Connection )resource; 50 try 51 { 52 if (con.getAutoCommit()) 53 { 54 con.setAutoCommit(false); 55 } 56 } 57 catch (SQLException e) 58 { 59 throw new TransactionException(new Message(Messages.TX_SET_AUTO_COMMIT_FAILED), e); 60 } 61 super.bindResource(key, resource); 62 } 63 64 69 protected void doBegin() throws TransactionException 70 { 71 } 73 74 79 protected void doCommit() throws TransactionException 80 { 81 try 82 { 83 ((Connection )resource).commit(); 84 ((Connection )resource).close(); 85 } 86 catch (SQLException e) 87 { 88 throw new TransactionException(new Message(Messages.TX_COMMIT_FAILED), e); 89 } 90 } 91 92 97 protected void doRollback() throws TransactionException 98 { 99 try 100 { 101 ((Connection )resource).rollback(); 102 ((Connection )resource).close(); 103 } 104 catch (SQLException e) 105 { 106 throw new TransactionRollbackException(new Message(Messages.TX_ROLLBACK_FAILED), e); 107 } 108 } 109 110 } 111 | Popular Tags |