Open Source Repository

Home /hibernate/hibernate-3.2.7.ga | Repository Home



org/hibernate/tool/hbm2ddl/SuppliedConnectionHelper.java
package org.hibernate.tool.hbm2ddl;

import org.hibernate.util.JDBCExceptionReporter;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * A {@link ConnectionHelper} implementation based on an explicitly supplied
 * connection.
 *
 @author Steve Ebersole
 */
class SuppliedConnectionHelper implements ConnectionHelper {
  private Connection connection;
  private boolean toggleAutoCommit;

  public SuppliedConnectionHelper(Connection connection) {
    this.connection = connection;
  }

  public void prepare(boolean needsAutoCommitthrows SQLException {
    toggleAutoCommit = needsAutoCommit && !connection.getAutoCommit();
    if toggleAutoCommit ) {
      try {
        connection.commit();
      }
      catchThrowable ignore ) {
        // might happen with a managed connection
      }
      connection.setAutoCommittrue );
    }
  }

  public Connection getConnection() {
    return connection;
  }

  public void release() throws SQLException {
    JDBCExceptionReporter.logAndClearWarningsconnection );
    if toggleAutoCommit ) {
      connection.setAutoCommitfalse );
    }
    connection = null;
  }
}