1 16 package com.ibatis.sqlmap.engine.transaction.external; 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 java.sql.Connection ; 27 import java.sql.SQLException ; 28 29 public class ExternalTransaction implements Transaction { 30 31 private static final Log connectionLog = LogFactory.getLog(Connection .class); 32 33 private DataSource dataSource; 34 private boolean defaultAutoCommit; 35 private boolean setAutoCommitAllowed; 36 private Connection connection; 37 private IsolationLevel isolationLevel = new IsolationLevel(); 38 39 public ExternalTransaction(DataSource ds, boolean defaultAutoCommit, boolean setAutoCommitAllowed, int isolationLevel) throws TransactionException { 40 dataSource = ds; 42 if (dataSource == null) { 43 throw new TransactionException("ExternalTransaction initialization failed. DataSource was null."); 44 } 45 46 this.defaultAutoCommit = defaultAutoCommit; 47 this.setAutoCommitAllowed = setAutoCommitAllowed; 48 this.isolationLevel.setIsolationLevel(isolationLevel); 49 } 50 51 private void init() throws SQLException , TransactionException { 52 connection = dataSource.getConnection(); 54 if (connection == null) { 55 throw new TransactionException("ExternalTransaction could not start transaction. Cause: The DataSource returned a null connection."); 56 } 57 isolationLevel.applyIsolationLevel(connection); 59 if (setAutoCommitAllowed) { 61 if (connection.getAutoCommit() != defaultAutoCommit) { 62 connection.setAutoCommit(defaultAutoCommit); 63 } 64 } 65 if (connectionLog.isDebugEnabled()) { 67 connection = ConnectionLogProxy.newInstance(connection); 68 } 69 } 70 71 public void commit() throws SQLException , TransactionException { 72 } 73 74 public void rollback() throws SQLException , TransactionException { 75 } 76 77 public void close() throws SQLException , TransactionException { 78 if (connection != null) { 79 try { 80 isolationLevel.restoreIsolationLevel(connection); 81 } finally { 82 connection.close(); 83 connection = null; 84 } 85 } 86 } 87 88 public Connection getConnection() throws SQLException , TransactionException { 89 if (connection == null) { 90 init(); 91 } 92 return connection; 93 } 94 95 } 96 | Popular Tags |