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

Support custom callback handlers #2448

Conversation

Randgalt
Copy link
Contributor

Currently, it is very simple to add global custom handling for
transaction callbacks using JDBI's TransactionHandler.
Specialized behavior can be added for an app's entire JDBI usage
without project devs needing to do anything special. This is very
useful when introducing protocols such as custom retry behavior,
custom connection management, etc.

Introduce new interface that is an analog of TransactionHandler
but for all callbacks, not just transactions. This interface,
Handler is used to invoke the callback passed to JDBI's
useHandle, withHandle, inTransaction and withTransaction.
This gives users a chance to global patch callbacks for all
JDBI uses not just transactions.

* @throws X any exception thrown by the callback.
* @see Jdbi#withHandle(HandleCallback)
*/
<R, X extends Exception> R withHandle(Handle handle,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the ability to decorate withHandle seems fine.

One thought: as this is right now, the Handler will remain in the stack trace, even if it does nothing.

An alternate implementation would be HandleCallback<R, X> decorate(HandleCallback<R, X> callback). Then, the default implementation would just return callback; and the stack frame would not show up in debuggers or profilers when it does nothing.

What do you think? I am fine with this approach too if you don't like the suggestion. cc @hgschmie

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's better. I've pushed that change.

@Randgalt Randgalt force-pushed the jordanz/support-custom-callback-handlers branch 3 times, most recently from e241960 to 0af874c Compare July 29, 2023 07:24
Introduce new interface that is an analog of `TransactionHandler`
but for all callbacks, not just transactions. This interface,
`Handler` is used to invoke the callback passed to JDBI's
`useHandle`, `withHandle`, `inTransaction` and `withTransaction`.
This gives users a chance to global patch callbacks for all
JDBI uses not just transactions.
@Randgalt Randgalt force-pushed the jordanz/support-custom-callback-handlers branch from 0af874c to 3e04d37 Compare July 30, 2023 09:00
@sonarcloud
Copy link

sonarcloud bot commented Jul 30, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@stevenschlansker stevenschlansker merged commit 2ef03ee into jdbi:master Jul 30, 2023
36 checks passed
stevenschlansker added a commit that referenced this pull request Jul 30, 2023
@Randgalt
Copy link
Contributor Author

Thank you!!!

@Randgalt
Copy link
Contributor Author

We'd appreciate a release when you can. Thank you in advance.

@Randgalt Randgalt deleted the jordanz/support-custom-callback-handlers branch August 1, 2023 13:27
@stevenschlansker
Copy link
Member

@Randgalt , released in 3.40.0

Randgalt added a commit to Randgalt/jdbi that referenced this pull request Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants