Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat JDBC connection pool (releasing connection)

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 says con.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 this aPool.

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) {}
            }
like image 273
Harsh .. Avatar asked May 07 '13 07:05

Harsh ..


People also ask

How do you release a connection pool?

You can disable Pooling by adding 'Pooling=false' to the connection string. In such case, a connection will be deleted from memory and free the session. However this may lead to performance loss. Most likely, pooling should not cause creating too much sessions.

How does Tomcat JDBC connection pool work?

Tomcat jdbc pool implements the ability retrieve a connection asynchronously, without adding additional threads to the library itself. Tomcat jdbc pool is a Tomcat module, it depends on Tomcat JULI, a simplified logging framework used in Tomcat. Retrieve the underlying connection using the javax. sql.

What are some of the main issues with using connection pools?

One of the most common issues undermining connection pool benefits is the fact that pooled connections can end up being stale. This most often happens due to inactive connections being timed out by network devices between the JVM and the database. As a result, there will be stale connections in the pool.

When should you not use connection pooling?

You reuse a prior database connection, in a new context to avoid the cost of setting up a new database connection for each request. The primary reason to avoid using database connections is that you're application's approach to solving problems isn't structured to accommodate a database connection pool.


2 Answers

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

like image 126
mschenk74 Avatar answered Oct 07 '22 01:10

mschenk74


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.

like image 38
Harsh .. Avatar answered Oct 07 '22 01:10

Harsh ..