javaoracle-databasejdbcucp

UCP algorithm of work


I work with Universal connection pool from Oracle. I work on this scheme

class Action {
  static PoolDataSource initPool() {
    PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
    pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
    pds.setURL(".........");
    pds.setUser("user");
    pds.setPassword("pass");
    pds.setInitialPoolSize(0);
    return pds;
  }

  static final PoolDataSource pds = initPool();

  void doAction() {
    Connection connection = pds.getConnection();
    ..........
    connection.close();  // ????
  }
}

Do you need to call connection.close() after the doAction completes or does the whole sense of working with the pool get lost and the connections there should remain open until they themselves close on timeout?


Solution

  • TL;DR. YES close them. It doesn't actually close, it returns to the connection pool for the next getConnection() call.

    The full doc is here: https://docs.oracle.com/cd/E11882_01/java.112/e12265/connect.htm#CHDJCGGB

    Borrowed connections that are no longer being used should be returned to the pool so that they can be available for the next connection request. The close method is used to close connections and automatically returns the connections to the pool. The close method does not physically remove the connection from the pool.

    Borrowed connections that are not closed will remain borrowed; subsequent requests for a connection result in a new connection being created if no connections are available. This behavior can cause many connections to be created and can affect system performance.