Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Handling Connections #38

Closed
scrayos opened this issue Feb 28, 2014 · 1 comment
Closed

[Question] Handling Connections #38

scrayos opened this issue Feb 28, 2014 · 1 comment
Labels

Comments

@scrayos
Copy link

scrayos commented Feb 28, 2014

Hey brettwooldridge,

I haven't worked with connectionpools a lot, and I stumbled upon the question what I have to do in the finally-block of queries. So without a connection-pool I closed the PreparedStatement, the ResultSet and the Connection. Which of them do I have to close in the finally-block while using HikariCP as ConnectionPool?

Thanks in Advance!
Scrayos

@brettwooldridge
Copy link
Owner

In general it is always a good practice to release resources like Statements and ResultSets quickly by explicitly closing them. Having said that, the contract of JDBC states that closing a Statement will automatically close open ResultSets, and that closing a Connection will automatically close open Statements. So in theory you only ever need to close the Connection in the finally, and it will close open Statements which will close open ResultSets.

In a Connection pool, even though closing a Connection does not truly close it, and instead returns it to the pool, a properly implemented connection pool should close open Statements in just the same way as standard JDBC when the connection is returned to the pool.

You can basically leave your code just as it is. One of the contracts of a connection pool is that changing from a standard DataSource to a pooled DataSource should require no changes to the code that uses it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants