Tomcat JDBC connection pool (releasing connection)

ε祈祈猫儿з 提交于 2019-12-05 05:25:26
mschenk74

Since you call the close() on a method obtained by the pool it is up to the pool what to do inside this method call. It does not neccessarily have to close the pooled database connection - it may do some cleanup and then add the connetion back to the pool.

This is already answered in Closing JDBC Connections in Pool

OK, my bad, that I did not see the implementation of DataSource. It extends DataSourceProxy that internally creates a pool before returning a Connectionbased on the PoolProperties

I understand, its upto this DataSource to handle the connections, even though I close the con in finally, DataSource may take necessary action.

Do add a comment/reply if anybody thinks otherwise.

That example only shows how to create and use a data source. For connection pool on Tomcat you may configure JNDI.

// Sample
public static Connection getConnectionFromPool() {
    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDB");
    return ds.getConnection();
    ...

Quote from How connection pooling works in Java and JDBC:

A connection pool operates by performing the work of creating connections ahead of time, In the case of a JDBC connection pool, a pool of Connection objects is created at the time the application server (or some other server) starts. These objects are then managed by a pool manager that disperses connections as they are requested by clients and returns them to the pool when it determines the client is finished with the Connection object. A great deal of housekeeping is involved in managing these connections.

When the connection pool server starts, it creates a predetermined number of Connection objects. A client application would then perform a JNDI lookup to retrieve a reference to a DataSource object that implements the ConnectionPoolDataSource interface. The client application would not need make any special provisions to use the pooled data source; the code would be no different from code written for a nonpooled DataSource.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!