I use Apache connection pool. Generally I close a connection after usage. Sometimes I can't reach an original connection, so I get it from a statement and close. I noticed that an original connection and one gotten from statement are not equal. Why it's so? Is any risk of connections leak when when I close connection from statement ?
I noticed that an original connection and one gotten from statement are not equal.
If you're using Commons pooling, the original one is a wrapped connection. The close() method is overridden to return the connection to the pool, in stead of closing the database connection.
When you're using the connection from the statement, it depends on the jdbc implementation. You might get the non-wrapped connection.
Closing it would not return the connection to the pool, and close the actual database connection.
You could test the scenario by creating a pool with one connection, - Get connection from the pool, - create a statement, - get the connection from the statement - close that connection. - Get a connection from the pool.
Yes, I use Commons. I remember when I implemented a pool, I had a background process watching for closed wrapped connections and removed them from pool. I'm expecting a similar mechanism from Commons. Do you know if there any?
No. You can provide a validation query. Commons uses it to test the state of your connection before handing it over. But there is (as far as I know) no mechanism to revoke wrapper connections that have not been returned to the pool.