View Javadoc
1   /*
2    * Copyright 2004-2025 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.jdbc;
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 JdbcTransaction.
32   */
33  public class JdbcTransaction 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 connection. */
42    private Connection connection;
43  
44    /** The isolation level. */
45    private IsolationLevel isolationLevel = new IsolationLevel();
46  
47    /**
48     * Instantiates a new jdbc transaction.
49     *
50     * @param ds
51     *          the ds
52     * @param isolationLevel
53     *          the isolation level
54     *
55     * @throws TransactionException
56     *           the transaction exception
57     */
58    public JdbcTransaction(DataSource ds, int isolationLevel) throws TransactionException {
59      // Check Parameters
60      dataSource = ds;
61      if (dataSource == null) {
62        throw new TransactionException("JdbcTransaction initialization failed.  DataSource was null.");
63      }
64      this.isolationLevel.setIsolationLevel(isolationLevel);
65    }
66  
67    /**
68     * Inits the.
69     *
70     * @throws SQLException
71     *           the SQL exception
72     * @throws TransactionException
73     *           the transaction exception
74     */
75    private void init() throws SQLException, TransactionException {
76      // Open JDBC Transaction
77      connection = dataSource.getConnection();
78      if (connection == null) {
79        throw new TransactionException(
80            "JdbcTransaction could not start transaction.  Cause: The DataSource returned a null connection.");
81      }
82      // Isolation Level
83      isolationLevel.applyIsolationLevel(connection);
84      // AutoCommit
85      if (connection.getAutoCommit()) {
86        connection.setAutoCommit(false);
87      }
88      // Debug
89      if (connectionLog.isDebugEnabled()) {
90        connection = ConnectionLogProxy.newInstance(connection);
91      }
92    }
93  
94    @Override
95    public void commit() throws SQLException, TransactionException {
96      if (connection != null) {
97        connection.commit();
98      }
99    }
100 
101   @Override
102   public void rollback() throws SQLException, TransactionException {
103     if (connection != null) {
104       connection.rollback();
105     }
106   }
107 
108   @Override
109   public void close() throws SQLException, TransactionException {
110     if (connection != null) {
111       try {
112         isolationLevel.restoreIsolationLevel(connection);
113       } finally {
114         connection.close();
115         connection = null;
116       }
117     }
118   }
119 
120   @Override
121   public Connection getConnection() throws SQLException, TransactionException {
122     if (connection == null) {
123       init();
124     }
125     return connection;
126   }
127 
128 }