/** Creates a Pooled connection and adds it to the connection pool. */
  private void installConnection() throws EmanagerDatabaseException {
    logger.debug("enter");

    PooledConnection connection;

    try {
      connection = poolDataSource.getPooledConnection();
      connection.addConnectionEventListener(this);
      connection.getConnection().setAutoCommit(false);
      synchronized (connectionPool) {
        connectionPool.add(connection);
        logger.debug("Database connection added.");
      }
    } catch (SQLException ex) {
      logger.fatal("exception caught while obtaining database " + "connection: ex = " + ex);
      SQLException ex1 = ex.getNextException();
      while (ex1 != null) {
        logger.fatal("chained sql exception ex1 = " + ex1);
        SQLException nextEx = ex1.getNextException();
        ex1 = nextEx;
      }
      String logString;
      EmanagerDatabaseException ede;

      logString =
          EmanagerDatabaseStatusCode.DatabaseConnectionFailure.getStatusCodeDescription()
              + ex.getMessage();

      logger.fatal(logString);
      ede =
          new EmanagerDatabaseException(
              EmanagerDatabaseStatusCode.DatabaseConnectionFailure, logString);
      throw ede;
    }
  }
  /**
   * Creates a new {@link PooledConnectionAndInfo} from the given {@link UserPassKey}.
   *
   * @param upkey {@link UserPassKey} containing user credentials
   * @throws SQLException if the connection could not be created.
   * @see org.apache.tomcat.dbcp.pool2.KeyedPooledObjectFactory#makeObject(java.lang.Object)
   */
  @Override
  public synchronized PooledObject<PooledConnectionAndInfo> makeObject(UserPassKey upkey)
      throws Exception {
    PooledConnectionAndInfo pci = null;

    PooledConnection pc = null;
    String username = upkey.getUsername();
    String password = upkey.getPassword();
    if (username == null) {
      pc = _cpds.getPooledConnection();
    } else {
      pc = _cpds.getPooledConnection(username, password);
    }

    if (pc == null) {
      throw new IllegalStateException(
          "Connection pool data source returned null from getPooledConnection");
    }

    // should we add this object as a listener or the pool.
    // consider the validateObject method in decision
    pc.addConnectionEventListener(this);
    pci = new PooledConnectionAndInfo(pc, username, password);
    pcMap.put(pc, pci);

    return new DefaultPooledObject<>(pci);
  }
  /**
   * @param key
   * @throws SQLException if the connection could not be created.
   * @see org.apache.commons.pool.KeyedPoolableObjectFactory#makeObject(java.lang.Object)
   */
  public synchronized Object makeObject(Object key) throws Exception {
    Object obj = null;
    UserPassKey upkey = (UserPassKey) key;

    PooledConnection pc = null;
    String username = upkey.getUsername();
    String password = upkey.getPassword();
    if (username == null) {
      pc = _cpds.getPooledConnection();
    } else {
      pc = _cpds.getPooledConnection(username, password);
    }

    if (pc == null) {
      throw new IllegalStateException(
          "Connection pool data source returned null from getPooledConnection");
    }

    // should we add this object as a listener or the pool.
    // consider the validateObject method in decision
    pc.addConnectionEventListener(this);
    obj = new PooledConnectionAndInfo(pc, username, password);
    pcMap.put(pc, obj);
    cleanupListeners();

    return obj;
  }
 private synchronized Connection getConnection3() throws SQLException {
   if (isDisposed) { // test again within synchronized lock
     throw new IllegalStateException("Connection pool has been disposed.");
   }
   PooledConnection pconn;
   if (!recycledConnections.isEmpty()) {
     pconn = recycledConnections.remove();
   } else {
     pconn = dataSource.getPooledConnection();
     pconn.addConnectionEventListener(poolConnectionEventListener);
   }
   Connection conn;
   try {
     // The JDBC driver may call ConnectionEventListener.connectionErrorOccurred()
     // from within PooledConnection.getConnection(). To detect this within
     // disposeConnection(), we temporarily set connectionInTransition.
     connectionInTransition = pconn;
     conn = pconn.getConnection();
   } finally {
     connectionInTransition = null;
   }
   activeConnections++;
   assertInnerState();
   return conn;
 }
  /**
   * @return Connection
   * @roseuid 3F3A5FFD0338
   */
  public Connection getConnection() throws EmanagerDatabaseException {
    long connectionId;
    Connection connection;
    PooledConnection pooledConnection;

    connection = null;
    pooledConnection = null;
    connectionId = InvalidConnectionId;

    try {
      synchronized (connectionPool) {
        if (!connectionPool.isEmpty()) {
          try {
            boolean connectionClosed;

            connectionClosed = false;

            pooledConnection = (PooledConnection) connectionPool.remove(0);
            connection = pooledConnection.getConnection();
            connection.clearWarnings();
            connectionId = getConnectionID(connection);
            connectionClosed = connection.isClosed();
            if (connectionId == InvalidConnectionId || connectionClosed == true) {
              logger.debug("Pooled connection closed.");
              connection = null;
            }
          } catch (SQLException sqe) {
            logger.debug("Pooled connection closed.");
            connection = null;
          }
        }
      }

      if (connection == null) {
        logger.debug("Getting a new connection.");
        pooledConnection = poolDataSource.getPooledConnection();
        pooledConnection.addConnectionEventListener(this);
        connection = pooledConnection.getConnection();
        connection.clearWarnings();
        connectionId = getConnectionID(connection);
      }
    } catch (SQLException sqe) {
      String logString;
      EmanagerDatabaseException ede;

      logString =
          EmanagerDatabaseStatusCode.UnableToGetPooledConnection.getStatusCodeDescription()
              + sqe.getMessage();

      logger.error(logString);
      ede =
          new EmanagerDatabaseException(
              EmanagerDatabaseStatusCode.UnableToGetPooledConnection, logString);
      throw ede;
    }

    if (connectionId == InvalidConnectionId) {
      EmanagerDatabaseException ede;
      ede =
          new EmanagerDatabaseException(
              EmanagerDatabaseStatusCode.UnableToGetPooledConnection,
              EmanagerDatabaseStatusCode.UnableToGetPooledConnection.getStatusCodeDescription());
      throw ede;
    }

    logger.debug(
        "\n*****************************"
            + "\nPooled Connection Init"
            + "\nCon ID:"
            + connectionId
            + "\nCon Object:"
            + pooledConnection
            + "\nPool Object:"
            + connection
            + "\n*****************************");

    return connection;
  }
 @Override
 public void addConnectionEventListener(ConnectionEventListener eventTarget) {
   passthru.addConnectionEventListener(eventTarget);
 }