View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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 org.apache.ibatis.transaction.managed;
17  
18  import java.sql.Connection;
19  import java.sql.SQLException;
20  
21  import javax.sql.DataSource;
22  
23  import org.apache.ibatis.logging.Log;
24  import org.apache.ibatis.logging.LogFactory;
25  import org.apache.ibatis.session.TransactionIsolationLevel;
26  import org.apache.ibatis.transaction.Transaction;
27  
28  /**
29   * {@link Transaction} that lets the container manage the full lifecycle of the transaction.
30   * Delays connection retrieval until getConnection() is called.
31   * Ignores all commit or rollback requests.
32   * By default, it closes the connection but can be configured not to do it.
33   *
34   * @author Clinton Begin
35   *
36   * @see ManagedTransactionFactory
37   */
38  public class ManagedTransaction implements Transaction {
39  
40    private static final Log log = LogFactory.getLog(ManagedTransaction.class);
41  
42    private DataSource dataSource;
43    private TransactionIsolationLevel level;
44    private Connection connection;
45    private final boolean closeConnection;
46  
47    public ManagedTransaction(Connection connection, boolean closeConnection) {
48      this.connection = connection;
49      this.closeConnection = closeConnection;
50    }
51  
52    public ManagedTransaction(DataSource ds, TransactionIsolationLevel level, boolean closeConnection) {
53      this.dataSource = ds;
54      this.level = level;
55      this.closeConnection = closeConnection;
56    }
57  
58    @Override
59    public Connection getConnection() throws SQLException {
60      if (this.connection == null) {
61        openConnection();
62      }
63      return this.connection;
64    }
65  
66    @Override
67    public void commit() throws SQLException {
68      // Does nothing
69    }
70  
71    @Override
72    public void rollback() throws SQLException {
73      // Does nothing
74    }
75  
76    @Override
77    public void close() throws SQLException {
78      if (this.closeConnection && this.connection != null) {
79        if (log.isDebugEnabled()) {
80          log.debug("Closing JDBC Connection [" + this.connection + "]");
81        }
82        this.connection.close();
83      }
84    }
85  
86    protected void openConnection() throws SQLException {
87      if (log.isDebugEnabled()) {
88        log.debug("Opening JDBC Connection");
89      }
90      this.connection = this.dataSource.getConnection();
91      if (this.level != null) {
92        this.connection.setTransactionIsolation(this.level.getLevel());
93      }
94    }
95  
96    @Override
97    public Integer getTimeout() throws SQLException {
98      return null;
99    }
100 
101 }