UCP算法的工作

UCP algorithm of work

我使用 Oracle 的通用连接池。我在研究这个方案

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();  // ????
  }
}

您是否需要在 doAction 完成后调用 connection.close(),或者是否失去了使用池的整体感觉并且那里的连接应该保持打开状态,直到它们自己在超时时关闭?

长话短说;博士。是关闭它们。它实际上并没有关闭,它 returns 到下一个 getConnection() 调用的连接池。

完整文档在这里: 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.