ryvantage
ryvantage

Reputation: 13489

JDBC Connection close vs abort

I asked this question (How do I call java.sql.Connection::abort?) and it led me to another question.

With

java.sql.Connection conn = ... ;

What is the difference between

conn.close();

and

conn.abort(...);

?

Upvotes: 2

Views: 2239

Answers (2)

Mark Rotteveel
Mark Rotteveel

Reputation: 108982

You use Connection.close() for a normal, synchronous, close of the connection. The abort method on the other hand is for abruptly terminating a connection that may be stuck.

In most cases you will need to use close(), but close() can sometimes not complete in time, for example it could block if the connection is currently busy (eg executing a long running query or update, or maybe waiting for a lock).

The abort method is for that situation: the driver will mark the connection as closed (hopefully) immediately, the method returns, and the driver can then use the provided Executor to asynchronously perform the necessary cleanup work (eg making sure the statement that is stuck gets aborted, cleaning up other resources, etc).

I hadn't joined the JSR-221 (JDBC specification) Expert Group yet when this method was defined, but as far as I'm aware, the primary intended users for this method is not so much application code, but connection pools, transaction managers and other connection management code that may want to forcibly end connections that are in use too long or 'stuck'.

That said, application code can use abort as well. It may be faster than close (depending on the implementation), but you won't get notified of problems during the asynchronous clean up, and you may abort current operations in progress.

However keep in mind, an abort is considered an abrupt termination of the connection, so it may be less graceful than a close, and it could lead to unspecified behaviour. Also, I'm not sure how well it is supported in drivers compared to a normal close().

Upvotes: 7

J E Carter II
J E Carter II

Reputation: 1496

Consulting the java docs seems to indicate that abort is more thorough than close, which is interesting.

abort...

Terminates an open connection. Calling abort results in: The connection marked as closed Closes any physical connection to the database Releases resources used by the connection Insures that any thread that is currently accessing the connection will either progress to completion or throw an SQLException.

close...

Releases this Connection object's database and JDBC resources immediately instead of waiting for them to be automatically released. Calling the method close on a Connection object that is already closed is a no-op.

So it seems if you are only concerned with releasing the objects, use close. If you want to make sure it's somewhat more "thread safe", using abort appears to provide a more graceful disconnect.

Per Mark Rotteveel's comment (which gives an accurate summary of the practical difference), my interpretation was incorrect.

Reference: https://docs.oracle.com/javase/8/docs/api/java/sql/Connection.html#close--

Upvotes: 0

Related Questions