We are seeing situations where our database connection from org.apache.commons.dbcp.BasicDataSource
is dying with socket write errors:
com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset by peer: socket write error
All subsequent attempts to write to the connection fail, of course:
com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
After updating the code to catch such exceptions and request a new connection when it occurs, it failed again. Am I correct in suspecting that calling DataSource#getConnection()
is not actually giving a new connection each time it is called? Isn't it just reusing the existing connection, which is closed?
If I am correct, what is the right way to throw away the old connection and request a new one?
EDIT: 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? 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();
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…