The pooled connection has been closed by the DB. That can mean 2 things.
The pooled connection has been closed by the DB. That can mean 2 things: The connection pool holds connections open for too long. The DB closes connections after a too short time.In theory, increasing/decreasing the timeout on either sides to align it should fix the problem.
On DBCP, your best bet is to validate connections before returning by a testOnBorrow=true and a validationQuery setting, e.g. SELECT 1. You can find configuration options in the Tomcat JDBC data sources documentation. Update as per your update: Here's a more succint version of what I'd like to know: Connection c1, c2; c1 = DatabaseManager.getConnection(); // c1.close() not called c2 = DatabaseManager.getConnection(); Is "c1 == c2" a true statement?
Or have two connections been allocated? It are two distinct connections. Only if you call c1.close() then there's a reasonable chance that c2 returns the same connection.
And if it's the latter, would code like this represent a "connection pool leak": Connection c1; c1 = DatabaseManager.getConnection(); // c1.close() not called c1 = DatabaseManager.getConnection(); Yes, definitely it will leak the first connection as it's never been returned to the pool. You should always close all DB resources in the shortest possible scope in a try-finally block. A bit decent connection pool is however configureable to reap abandoned connections, but this should definitely not be used as "workaround".
– JimBurnell Sep 8 at 15:52 No, closing a pooled connection will just return it to the pool (that's why you should ALWAYS close connections in finally, else pool may run out of connections). Remnant of handling the broken connections is up to the pool which you should control by configuration. You should however realize that DBCP is a poorly implemented pool.
See also stackoverflow.Com/questions/520585/… – BalusC Sep 8 at 15:54 But what if close() is never called? What if the code just keeps calling getConnection()? Does it still re-use the old, closed connection, or does it get a new connection from the pool?
– JimBurnell Sep 8 at 16:00 If you never call Connection#close() (and Statement#close() and ResultSet#close()), then you've bigger problems with your application. Closing a pooled connection will return it to the pool. Not doing so will accumulate the connections until DB runs out of them (which in turn may force closing of older connections to reclaim them).
See also stackoverflow. Com/questions/5602772/… and stackoverflow. Com/questions/2313197/… – BalusC Sep 8 at 16:01 Thanks for the information.
If you don't mind, could I ask one more clarifying question? Connection c1, c2; c1 = DatabaseManager.getConnection(); // c1.close() not called c2 = DatabaseManager.getConnection(); Is "c1 == c2" a true statement? Or have two connections been allocated?
And if it's the latter, would code like this represent a "connection pool leak": Connection c1; c1 = DatabaseManager.getConnection(); // c1.close() not called c1 = DatabaseManager.getConnection(); Sorry for all the questions – JimBurnell Sep 8 at 17:23.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.