Tomcat JDBC connection pool (releasing connection)
Solution 1
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
Solution 2
OK, my bad, that I did not see the implementation of DataSource
.
It extends DataSourceProxy
that internally creates a pool before returning a Connection
based 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.
Solution 3
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.
Related videos on Youtube
Harsh ..
Updated on September 15, 2022Comments
-
Harsh .. over 1 year
Referring to Tomcat JBDC connection pool, I see in the standalone java example given there, one gets the connection using
datasource.getConnection()
which is cool. But in the finally block, it sayscon.close()
.Question: When I implement this, it seems obvious that the
con
I get from datasource will be closed every time in the finally. When this is closed, will the connection pooling mechanism acquire a new connection and adds it to the pool?I presume there should be a method call like
releaseConnection()
that will let the pool take its own decision whether to close it or let it be open for some other use.I've also tried doing this
ConnectionPool aPool = datasource.createPool();
But I see there is nothing like release connection on thisaPool
.I think I'm missing something here? Appreciate your help.
Code snippet from Tomcat JBDC connection pool:
DataSource datasource = new DataSource(); datasource.setPoolProperties(p); Connection con = null; try { con = datasource.getConnection(); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select * from user"); int cnt = 1; while (rs.next()) { System.out.println((cnt++)+". Host:" +rs.getString("Host")+ " User:"+rs.getString("User")+" Password:"+rs.getString("Password")); } rs.close(); st.close(); } finally { if (con!=null) try {con.close();}catch (Exception ignore) {} }
-
Hartmut Pfarr over 9 yearsKeep "MaxIdle" setting of DataSource high enough to get those connections not being closed. Setting it to 0 will shutdown any closed connection, I guess.