1 16 package com.ibatis.sqlmap.engine.transaction.jta; 17 18 import com.ibatis.common.jdbc.logging.ConnectionLogProxy; 19 import com.ibatis.common.logging.Log; 20 import com.ibatis.common.logging.LogFactory; 21 import com.ibatis.sqlmap.engine.transaction.IsolationLevel; 22 import com.ibatis.sqlmap.engine.transaction.Transaction; 23 import com.ibatis.sqlmap.engine.transaction.TransactionException; 24 25 import javax.sql.DataSource ; 26 import javax.transaction.Status ; 27 import javax.transaction.UserTransaction ; 28 import java.sql.Connection ; 29 import java.sql.SQLException ; 30 31 public class JtaTransaction implements Transaction { 32 33 private static final Log connectionLog = LogFactory.getLog(Connection .class); 34 35 private UserTransaction userTransaction; 36 private DataSource dataSource; 37 private Connection connection; 38 private IsolationLevel isolationLevel = new IsolationLevel(); 39 40 private boolean commmitted = false; 41 private boolean newTransaction = false; 42 43 public JtaTransaction(UserTransaction utx, DataSource ds, int isolationLevel) throws TransactionException { 44 userTransaction = utx; 46 dataSource = ds; 47 if (userTransaction == null) { 48 throw new TransactionException("JtaTransaction initialization failed. UserTransaction was null."); 49 } 50 if (dataSource == null) { 51 throw new TransactionException("JtaTransaction initialization failed. DataSource was null."); 52 } 53 this.isolationLevel.setIsolationLevel(isolationLevel); 54 } 55 56 private void init() throws TransactionException, SQLException { 57 try { 59 newTransaction = userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION; 60 if (newTransaction) { 61 userTransaction.begin(); 62 } 63 } catch (Exception e) { 64 throw new TransactionException("JtaTransaction could not start transaction. Cause: ", e); 65 } 66 67 connection = dataSource.getConnection(); 69 if (connection == null) { 70 throw new TransactionException("JtaTransaction could not start transaction. Cause: The DataSource returned a null connection."); 71 } 72 isolationLevel.applyIsolationLevel(connection); 74 if (connection.getAutoCommit()) { 76 connection.setAutoCommit(false); 77 } 78 if (connectionLog.isDebugEnabled()) { 80 connection = ConnectionLogProxy.newInstance(connection); 81 } 82 } 83 84 public void commit() throws SQLException , TransactionException { 85 if (connection != null) { 86 if (commmitted) { 87 throw new TransactionException("JtaTransaction could not commit because this transaction has already been committed."); 88 } 89 try { 90 if (newTransaction) { 91 userTransaction.commit(); 92 } 93 } catch (Exception e) { 94 throw new TransactionException("JtaTransaction could not commit. Cause: ", e); 95 } 96 commmitted = true; 97 } 98 } 99 100 public void rollback() throws SQLException , TransactionException { 101 if (connection != null) { 102 if (!commmitted) { 103 try { 104 if (userTransaction != null) { 105 if (newTransaction) { 106 userTransaction.rollback(); 107 } else { 108 userTransaction.setRollbackOnly(); 109 } 110 } 111 } catch (Exception e) { 112 throw new TransactionException("JtaTransaction could not rollback. Cause: ", e); 113 } 114 } 115 } 116 } 117 118 public void close() throws SQLException , TransactionException { 119 if (connection != null) { 120 try { 121 isolationLevel.restoreIsolationLevel(connection); 122 } finally { 123 connection.close(); 124 connection = null; 125 } 126 } 127 } 128 129 public Connection getConnection() throws SQLException , TransactionException { 130 if (connection == null) { 131 init(); 132 } 133 return connection; 134 } 135 136 137 } 138 | Popular Tags |