Exemplo n.º 1
0
  @Override
  public void setConnection(final DatabaseConnection conn) {
    LogFactory.getLogger()
        .debug("Connected to " + conn.getConnectionUserName() + "@" + conn.getURL());
    this.connection = conn;
    try {
      boolean autoCommit = conn.getAutoCommit();
      if (autoCommit == getAutoCommitMode()) {
        // Don't adjust the auto-commit mode if it's already what the database wants it to be.
        LogFactory.getLogger()
            .debug("Not adjusting the auto commit mode; it is already " + autoCommit);
      } else {
        // Store the previous auto-commit mode, because the connection needs to be restored to it
        // when this
        // AbstractDatabase type is closed. This is important for systems which use connection
        // pools.
        previousAutoCommit = autoCommit;

        LogFactory.getLogger()
            .debug("Setting auto commit to " + getAutoCommitMode() + " from " + autoCommit);
        connection.setAutoCommit(getAutoCommitMode());
      }
    } catch (DatabaseException e) {
      LogFactory.getLogger()
          .warning("Cannot set auto commit to " + getAutoCommitMode() + " on connection");
    }

    this.connection.attached(this);
  }
Exemplo n.º 2
0
  @Override
  public void close() throws DatabaseException {
    DatabaseConnection connection = getConnection();
    if (connection != null) {
      if (previousAutoCommit != null) {
        try {
          connection.setAutoCommit(previousAutoCommit);
        } catch (DatabaseException e) {
          LogFactory.getLogger()
              .warning("Failed to restore the auto commit to " + previousAutoCommit);

          throw e;
        }
      }
      connection.close();
    }
    ExecutorService.getInstance().clearExecutor(this);
  }