예제 #1
0
  /**
   * Get Connection
   *
   * @return connection
   */
  public Connection getConnection() {
    log.trace("Active={}, Connection={}", new Object[] {isActive(), m_connection});

    // metas: tsa: begin: Handle the case when the connection was already closed
    // Example: one case when we can get this is when we start a process with a transaction
    // and that process is commiting the transaction somewhere
    if (m_connection != null) {
      boolean isClosed = false;
      try {
        isClosed = m_connection.isClosed();
      } catch (SQLException e) {
        log.error(
            "Error checking if the connection is closed. Assume closed - "
                + e.getLocalizedMessage(),
            e);
        isClosed = true;
      }
      if (isClosed) {
        log.info("Connection is closed. Trying to create another connection.");
        m_connection = null;
      }
    }
    // metas: tsa: end:
    if (m_connection == null) // get new Connection
    {
      setConnection(DB.createConnection(isAutoCommit(), Connection.TRANSACTION_READ_COMMITTED));
    }
    if (!isActive()) {
      start();
    }

    try {
      m_connection.setAutoCommit(isAutoCommit());
    } catch (SQLException e) {
      throw DBException.wrapIfNeeded(e);
    }

    return m_connection;
  } // getConnection
  @Override
  public DBException wrapIfNeededOrReturnNull(final Throwable t) {
    final Boolean referencingTableHasDLMLevel;

    if (DBException.isSQLState(t, PG_SQLSTATE_Referencing_Record_Has_Wrong_DLM_Level)) {
      referencingTableHasDLMLevel = true;
    } else if (DBException.isSQLState(t, PG_SQLSTATE_Referencing_Table_Has_No_DLM_LEvel)) {
      referencingTableHasDLMLevel = false;
    } else {
      return null;
    }

    //
    // parse the exception detail and extract the infos
    final SQLException sqlException = DBException.extractSQLExceptionOrNull(t);
    Check.errorUnless(
        sqlException instanceof PSQLException,
        "exception={} needs to be a PSQLExcetion",
        sqlException);

    final PSQLException psqlException = (PSQLException) sqlException;
    final ServerErrorMessage serverErrorMessage = psqlException.getServerErrorMessage();
    Check.errorIf(
        serverErrorMessage == null,
        "ServerErrorMessage of PSQLException={} may not be null",
        psqlException);

    final String detail = serverErrorMessage.getDetail();
    Check.errorIf(
        Check.isEmpty(detail, true),
        "DETAIL ServerErrorMessage={} from of PSQLException={} may not be null",
        serverErrorMessage,
        psqlException);

    final String[] infos = extractInfos(detail);

    //
    // the the "real" tables and column from the extracted lowercase infos
    final IADTableDAO adTableDAO = Services.get(IADTableDAO.class);

    final I_AD_Table referencedTable = adTableDAO.retrieveTable(infos[0]);
    Check.errorIf(
        referencedTable == null,
        "Unable to retrieve an AD_Table for referencedTable name={}",
        infos[0]);

    final I_AD_Table referencingTable = adTableDAO.retrieveTable(infos[2]);
    Check.errorIf(
        referencingTable == null,
        "Unable to retrieve an AD_Table for referencingTable name={}",
        infos[2]);

    final I_AD_Column referencingColumn =
        adTableDAO.retrieveColumn(referencingTable.getTableName(), infos[3]);
    Check.errorIf(
        referencingTable == null,
        "Unable to retrieve an AD_Column for referencingTable name={} and referencingColumn name={}",
        infos[2],
        infos[3]);

    return new DLMReferenceException(
        t,
        TableReferenceDescriptor.of(
            referencingTable.getTableName(),
            referencingColumn.getColumnName(),
            referencedTable.getTableName(),
            Integer.parseInt(infos[1])),
        referencingTableHasDLMLevel);
  }
예제 #3
0
  /**
   * Get Cached Connection
   *
   * @param connection info
   * @param autoCommit true if autocommit connection
   * @param transactionIsolation Connection transaction level
   * @return connection or null
   * @throws Exception
   */
  @Override
  public Connection getCachedConnection(
      CConnection connection, boolean autoCommit, int transactionIsolation) throws Exception {
    Connection conn = null;
    Exception exception = null;
    try {
      if (m_ds == null) getDataSource(connection);

      //
      try {
        conn = m_ds.getConnection();
        if (conn != null) {
          if (conn.getTransactionIsolation() != transactionIsolation)
            conn.setTransactionIsolation(transactionIsolation);
          if (conn.getAutoCommit() != autoCommit) conn.setAutoCommit(autoCommit);
          //                      conn.setDefaultRowPrefetch(20);     //  10 default - reduces round
          // trips
        }
      } catch (Exception e) {
        exception = e;
        conn = null;
        if (DBException.isInvalidUserPassError(e)) {
          // log might cause infinite loop since it will try to acquire database connection again
          /*
          log.severe("Cannot connect to database: "
              + getConnectionURL(connection)
              + " - UserID=" + connection.getDbUid());
          */
          System.err.println(
              "Cannot connect to database: "
                  + getConnectionURL(connection)
                  + " - UserID="
                  + connection.getDbUid());
        }
      }

      if (conn == null && exception != null) {
        // log might cause infinite loop since it will try to acquire database connection again
        /*
        log.log(Level.SEVERE, exception.toString());
        log.fine(toString()); */
        System.err.println(exception.toString());
      }
    } catch (Exception e) {
      exception = e;
    }

    try {
      if (conn != null) {
        int numConnections = m_ds.getNumBusyConnections();
        if (numConnections >= m_maxbusyconnections && m_maxbusyconnections > 0) {
          log.warning(getStatus());
          // hengsin: make a best effort to reclaim leak connection
          Runtime.getRuntime().runFinalization();
        }
      }
    } catch (Exception ex) {

    }
    if (exception != null) throw exception;
    return conn;
  } //  getCachedConnection