ManagedTransaction.java

  1. /*
  2.  *    Copyright 2009-2024 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. import java.sql.Connection;
  18. import java.sql.SQLException;

  19. import javax.sql.DataSource;

  20. import org.apache.ibatis.logging.Log;
  21. import org.apache.ibatis.logging.LogFactory;
  22. import org.apache.ibatis.session.TransactionIsolationLevel;
  23. import org.apache.ibatis.transaction.Transaction;

  24. /**
  25.  * {@link Transaction} that lets the container manage the full lifecycle of the transaction. Delays connection retrieval
  26.  * until getConnection() is called. Ignores all commit or rollback requests. By default, it closes the connection but
  27.  * can be configured not to do it.
  28.  *
  29.  * @author Clinton Begin
  30.  *
  31.  * @see ManagedTransactionFactory
  32.  */
  33. public class ManagedTransaction implements Transaction {

  34.   private static final Log log = LogFactory.getLog(ManagedTransaction.class);

  35.   private DataSource dataSource;
  36.   private TransactionIsolationLevel level;
  37.   private Connection connection;
  38.   private final boolean closeConnection;

  39.   public ManagedTransaction(Connection connection, boolean closeConnection) {
  40.     this.connection = connection;
  41.     this.closeConnection = closeConnection;
  42.   }

  43.   public ManagedTransaction(DataSource ds, TransactionIsolationLevel level, boolean closeConnection) {
  44.     this.dataSource = ds;
  45.     this.level = level;
  46.     this.closeConnection = closeConnection;
  47.   }

  48.   @Override
  49.   public Connection getConnection() throws SQLException {
  50.     if (this.connection == null) {
  51.       openConnection();
  52.     }
  53.     return this.connection;
  54.   }

  55.   @Override
  56.   public void commit() throws SQLException {
  57.     // Does nothing
  58.   }

  59.   @Override
  60.   public void rollback() throws SQLException {
  61.     // Does nothing
  62.   }

  63.   @Override
  64.   public void close() throws SQLException {
  65.     if (this.closeConnection && this.connection != null) {
  66.       if (log.isDebugEnabled()) {
  67.         log.debug("Closing JDBC Connection [" + this.connection + "]");
  68.       }
  69.       this.connection.close();
  70.     }
  71.   }

  72.   protected void openConnection() throws SQLException {
  73.     if (log.isDebugEnabled()) {
  74.       log.debug("Opening JDBC Connection");
  75.     }
  76.     this.connection = this.dataSource.getConnection();
  77.     if (this.level != null) {
  78.       this.connection.setTransactionIsolation(this.level.getLevel());
  79.     }
  80.   }

  81.   @Override
  82.   public Integer getTimeout() throws SQLException {
  83.     return null;
  84.   }

  85. }