Search code examples
javadatabasejdbcconnectionconnection-pooling

Does killing a session at the database server side also closes the connection and makes the jdbc connection object null?


Suppose there is a jdbc code running some CRUD operations. Now, in a particular scenario there happened to be a row lock and the session became blocking to other database sessions.

In this case if the blocking session was killed at the database side what would happen to the corresponding java Connection object? Would that object become null after giving exception? What could be the behaviour if that connection object is used later in the code without placing the checks?

I know that as best practice we should get connection from the pool and close it when finished, but there is quite a lot of legacy code written the other way which will take some time to clean-up.

What would be the answer if the above connection is created through a datasource, managing connection pool, rather than through plain jdbc connection?


Solution

  • Whatever operations are in progress will throw an SQLException and any calls to the Connection.isClosed() method will return true. Any subsequent attempts to use the connection object will also throw SQLException with the error saying that the connection is closed.

    This will not set your connection object to null. Nothing can do this unless you write explicit code to set the object to null if an exception is thrown or Connection.isClosed() returns true. You would have to do that manually.