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