View Javadoc
1   /*
2    * Copyright 2004-2026 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
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 java.sql.Connection;
26  import java.sql.SQLException;
27  
28  import javax.sql.DataSource;
29  
30  /**
31   * The Class ExternalTransaction.
32   */
33  public class ExternalTransaction implements Transaction {
34  
35    /** The Constant connectionLog. */
36    private static final Log connectionLog = LogFactory.getLog(Connection.class);
37  
38    /** The data source. */
39    private DataSource dataSource;
40  
41    /** The default auto commit. */
42    private boolean defaultAutoCommit;
43  
44    /** The set auto commit allowed. */
45    private boolean setAutoCommitAllowed;
46  
47    /** The connection. */
48    private Connection connection;
49  
50    /** The isolation level. */
51    private IsolationLevel isolationLevel = new IsolationLevel();
52  
53    /**
54     * Instantiates a new external transaction.
55     *
56     * @param ds
57     *          the ds
58     * @param defaultAutoCommit
59     *          the default auto commit
60     * @param setAutoCommitAllowed
61     *          the set auto commit allowed
62     * @param isolationLevel
63     *          the isolation level
64     *
65     * @throws TransactionException
66     *           the transaction exception
67     */
68    public ExternalTransaction(DataSource ds, boolean defaultAutoCommit, boolean setAutoCommitAllowed, int isolationLevel)
69        throws TransactionException {
70      // Check Parameters
71      dataSource = ds;
72      if (dataSource == null) {
73        throw new TransactionException("ExternalTransaction initialization failed.  DataSource was null.");
74      }
75  
76      this.defaultAutoCommit = defaultAutoCommit;
77      this.setAutoCommitAllowed = setAutoCommitAllowed;
78      this.isolationLevel.setIsolationLevel(isolationLevel);
79    }
80  
81    /**
82     * Inits the.
83     *
84     * @throws SQLException
85     *           the SQL exception
86     * @throws TransactionException
87     *           the transaction exception
88     */
89    private void init() throws SQLException, TransactionException {
90      // Open JDBC Transaction
91      connection = dataSource.getConnection();
92      if (connection == null) {
93        throw new TransactionException(
94            "ExternalTransaction could not start transaction.  Cause: The DataSource returned a null connection.");
95      }
96      // Isolation Level
97      isolationLevel.applyIsolationLevel(connection);
98      // AutoCommit
99      if (setAutoCommitAllowed && connection.getAutoCommit() != defaultAutoCommit) {
100       connection.setAutoCommit(defaultAutoCommit);
101     }
102     // Debug
103     if (connectionLog.isDebugEnabled()) {
104       connection = ConnectionLogProxy.newInstance(connection);
105     }
106   }
107 
108   @Override
109   public void commit() throws SQLException, TransactionException {
110   }
111 
112   @Override
113   public void rollback() throws SQLException, TransactionException {
114   }
115 
116   @Override
117   public void close() throws SQLException, TransactionException {
118     if (connection != null) {
119       try {
120         isolationLevel.restoreIsolationLevel(connection);
121       } finally {
122         connection.close();
123         connection = null;
124       }
125     }
126   }
127 
128   @Override
129   public Connection getConnection() throws SQLException, TransactionException {
130     if (connection == null) {
131       init();
132     }
133     return connection;
134   }
135 
136 }