Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if you close a closed connection?

I was reading my database access logic, and found that it was possible for the same connection to be closed twice in a row.

Other than being a duplicate instruction, is there any reason I should be concerned about closing a connection after it's already been closed?

like image 588
Zibbobz Avatar asked Jun 12 '15 14:06

Zibbobz


2 Answers

If you are using the java.sql.Connection you should have no issue.

From the Connection documentation:

Calling the method close on a Connection object that is already closed is a no-op.

i.e. it does nothing.

This should stand for any proper implementation. Although it is conceivable for some implementation to have odd behavior on this matter.

like image 190
Laurentiu L. Avatar answered Sep 29 '22 14:09

Laurentiu L.


From the documentation:

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. It is strongly recommended that an application explicitly commits or rolls back an active transaction prior to calling the close method. If the close method is called and there is an active transaction, the results are implementation-defined.

So in short it is supposed to do nothing. Be aware though that a sloppy implementation for this Connection interface might fail to meet the rules defined in this interface's contract. You did not say which database are you using so I cannot provide further information on the implementation details.

like image 43
Adam Arold Avatar answered Sep 29 '22 14:09

Adam Arold