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