1 16 package com.ibatis.dao.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.dao.client.DaoException; 22 import com.ibatis.dao.engine.transaction.ConnectionDaoTransaction; 23 24 import javax.sql.DataSource ; 25 import javax.transaction.Status ; 26 import javax.transaction.UserTransaction ; 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 30 public class JtaDaoTransaction implements ConnectionDaoTransaction { 31 32 private static final Log connectionLog = LogFactory.getLog(Connection .class); 33 34 private UserTransaction userTransaction; 35 private DataSource dataSource; 36 private Connection connection; 37 38 private boolean commmitted = false; 39 private boolean newTransaction = false; 40 41 public JtaDaoTransaction(UserTransaction utx, DataSource ds) { 42 userTransaction = utx; 44 dataSource = ds; 45 if (userTransaction == null) { 46 throw new DaoException("JtaTransaction initialization failed. UserTransaction was null."); 47 } 48 if (dataSource == null) { 49 throw new DaoException("JtaTransaction initialization failed. DataSource was null."); 50 } 51 52 try { 54 newTransaction = userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION; 55 if (newTransaction) { 56 userTransaction.begin(); 57 } 58 } catch (Exception e) { 59 throw new DaoException("JtaTransaction could not start transaction. Cause: ", e); 60 } 61 62 try { 63 connection = dataSource.getConnection(); 65 if (connection == null) { 66 throw new DaoException("Could not start transaction. Cause: The DataSource returned a null connection."); 67 } 68 if (connection.getAutoCommit()) { 69 connection.setAutoCommit(false); 70 } 71 if (connectionLog.isDebugEnabled()) { 72 connection = ConnectionLogProxy.newInstance(connection); 73 } 74 } catch (SQLException e) { 75 throw new DaoException("Error opening JDBC connection. Cause: " + e, e); 76 } 77 } 78 79 public void commit() { 80 if (commmitted) { 81 throw new DaoException("JtaTransaction could not commit because this transaction has already been committed."); 82 } 83 try { 84 try { 85 if (newTransaction) { 86 userTransaction.commit(); 87 } 88 } finally { 89 close(); 90 } 91 } catch (Exception e) { 92 throw new DaoException("JtaTransaction could not commit. Cause: ", e); 93 } 94 commmitted = true; 95 } 96 97 public void rollback() { 98 if (!commmitted) { 99 try { 100 try { 101 if (userTransaction != null) { 102 if (newTransaction) { 103 userTransaction.rollback(); 104 } else { 105 userTransaction.setRollbackOnly(); 106 } 107 } 108 } finally { 109 close(); 110 } 111 } catch (Exception e) { 112 throw new DaoException("JTA transaction could not rollback. Cause: ", e); 113 } 114 } 115 116 } 117 118 119 public void close() throws SQLException { 120 if (connection != null) { 121 connection.close(); 122 connection = null; 123 } 124 } 125 126 public Connection getConnection() { 127 return connection; 128 } 129 130 131 } 132 | Popular Tags |