5

I have a project that uses Tomcat JDBC connection pool.

According to the JDK specification, the Connection.close() method should release the resources of the connection, like Statement and ResultSet.

I know that when using connection from pool, the close() method will be overriden to release the connection back to pool. I wonder if this overridden method should properly close the related resources or not.

Tomcat JDBC connection pool does not release the resources, and I need to explicitly close all resources in my code.

Are there any other pool implementation that properly close all related resources when calling Connection.close()?

AdamSkywalker
  • 11,408
  • 3
  • 38
  • 76
Ivan
  • 417
  • 1
  • 4
  • 10
  • Why do you *need* to close them in your code? – meskobalazs Jul 15 '15 at 07:47
  • 3
    Why would you want to close the connections and flush them out the pool ? The whole point of having a connection pool is to have ready, established connections that can be used whenever needed without the time-consuming connection-establishing. It comes with Tomcat for a reason, don't try to kill the core. – Christophe Schutz Jul 15 '15 at 07:50
  • 1
    You are right, a proper connection pool implementation would close the result sets, statements and other resources when the connection is returned to the connection pool by the call to `Connection.close()`. – Mark Rotteveel Jul 15 '15 at 07:51
  • 2
    @ChristopheSchutz I fail to see what part of the question your comment addresses. Closing a (pooled) connection returns it to the pool. And according to the specification a connection close must close related resources (result set, statement, etc), even if the physical connection is not really closed, the logical connection handed out by the connection pool **is** being closed. – Mark Rotteveel Jul 15 '15 at 07:53
  • 1
    You should never rely on that behaviour of the `Connection` and should **correctly** close all `(Auto)Closeable` resources in a `try-with-finally`. It is very likely that Tomcat JDBC uses `Statement` caching too, so closing a `Statement` does not close it either. The behaviour of connection pools is far more complex than you understand and I assure you that the Tomcat JDBC pool functions as intended. – Boris the Spider Jul 15 '15 at 07:55
  • Look into this explanation: http://stackoverflow.com/questions/4938517/closing-jdbc-connections-in-pool – Paweł Głowacz Jul 15 '15 at 08:15
  • @BoristheSpider It may behave as the developers intended, but it does violate the JDBC specification by failing to close the resources when the logical connection is closed and the physical connection is returned to the pool (and with statement caching, the logical statement still needs to be closed, while the physical statement is kept open). – Mark Rotteveel Jul 15 '15 at 08:56
  • @Mark , thanks and you have the same point of view as me. My project have a helper class to acquire connection from database, developer don't know that it's a connection from pool, and just close the connection only. The problem occur with SQL exception. Cursor not closed. I wonder if I should fire a bug to Tomcat. – Ivan Jul 15 '15 at 11:46

1 Answers1

0

You can configure tomcat-jdbc with interceptor. The one you are interrested in is StatementFinalizer

http://tomcat.apache.org/tomcat-8.0-doc/jdbc-pool.html#org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer

seneque
  • 279
  • 1
  • 5