Anirban
Anirban

Reputation: 949

Proper way to close connection

We are using connection pool in our project. We see in our project the statements are closed after the connection is closed. I know that in case of a connection pool, after a connection is closed the physical connection to the database is not closed but returns to the pool for reuse. So my question is:

What will happen if statements are closed after a connection is closed? Will the statements be closed properly/will closing the connection close all the statements and closing the statements are redundant/the statements are open and though the connection returns to the pool, it is not reusable because of open statements? (We are using both Statement and PreparedStatement).

Upvotes: 0

Views: 428

Answers (1)

user207421
user207421

Reputation: 311031

What will happen if statements are closed after a connection is closed?

Nothing. They were already closed. This is documented.

Will the statements be closed properly

Yes. This is documented.

/will closing the connection close all the statements

Yes. This is documented.

and closing the statements are redundant

Yes.

the statements are open and though the connection returns to the pool, it is not reusable because of open statements? (We are using both Statement and PreparedStatement).

I cannot make head or tail of this. If the connection is closed, the Statements and ResultSets derived from it are also closed. This is documented. If the Connection isn't closed, the Statements and ResultSets aren't closed either, unless you close them.

You must organise your code so that you don't rely on ResultSets beyond the life of the Statement, and Statement beyond the life of the Connection. This is trivially easy.

Upvotes: 1

Related Questions