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
Use attachToHandleForCleanup() everywhere #2293
Comments
Hi @jodastephen , Thank you for opening an issue with the Jdbi project! This is an interesting proposal. We could add a flag to |
As a caveat: Adding every statement to a handle for cleanup means that resources may be held for a while (until the handle is actually closed). There is a chance that these resources "pile up", especially when many statements are executed. |
adding the flag as proposed should do the trick. If you have a factory that creates the Jdbi objects, it should just call public class AttachHandlesPlugin extends JdbiPlugin.Singleton {
@Override
public void customizeJdbi(Jdbi jdbi) {
jdbi.getConfig(SqlStatements.class).setAttachAllStatementsForCleanup(true);
}
} and then call |
If you want to avoid piling up resources, we might be able to use a PhantomReference to allow references to be cleaned up earlier than handle close. |
A flag on As I mentioned above, there may be a case for callback here, as the Java 8+ design alternative to try-with-resources:
My thought process being that it is surprising to see try-with-resources in the middle of JDBI code, which is all lambda-based. |
I agree, if we want users to close all statements, providing a callback-based alternative to t-w-r does look nice. |
When I rewrote the code in 3.35.0 (which introduced the |
This will be addressed in the next release through #2294. See https://jdbi.org/#_attaching_statements_to_the_handle_lifecycle for a preview of the release documentation. In a nutshell: If you use callbacks on the jdbi object, it will "just work" even if you don't attach the statement to the handle or do any t-w-r resource management. Outside these callbacks, there is a global flag that you can set to get this behavior everywhere. |
Looks fabulous, thanks |
The method
attachToHandleForCleanup()
has been added recently, and looks to be a good solution to always ensuring things are closed. I could be wrong, but it seems like it might be desirable to setattachToHandleForCleanup()
always, however I can see no easy way to do this. ie. I can see no easy way at the framework-level, obviously I could update all the use sites, but adding try-with-resources is a fair bit more verbose. I don't believeStatementCustomizer
can do the job here.Some possible approaches I came up with:
Handle
-attachStatementsToHandleForCleanup()
that is used to auto-set the flag whenever aBaseStatement
subclass is created - feels quite nice, and can be configured inJdbiPlugin
JdbiPlugin
wheneverHandle
creates aQuery
/Update
/Call
etc. - not sure what else the callbacks would be used forHandle
that callsattachToHandleForCleanup()
after creating the statement - doesn't feel like JDBI design stylewithQuery(String, QueryCallback)
toHandle
- feels like overkillBackground info:
attachToHandleForCleanup()
thereThanks for JDBI!
The text was updated successfully, but these errors were encountered: