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

add EntityManager.runWithConnection()/callWithConnection() #433

Merged
merged 2 commits into from
Aug 21, 2023

Conversation

gavinking
Copy link
Contributor

see #432

@Tomas-Kraus
Copy link
Contributor

This is JPA so this looks like a h4ck3rs tool to bypass JPA API and execute whatever user wants directly trough java.sql. :)
Also EntityManager has unwrap for years and java.sql.Connection can be just added as mandatory here.

...so this is why I may have some concerns when talking about pure JPA API, but on the other hand everyone knows that users will always find a way to get Connection from EM for many purposes. And this will make it easy.
So let's make all those users happy.

@gavinking
Copy link
Contributor Author

This is JPA so this looks like a h4ck3rs tool to bypass JPA API and execute whatever user wants directly trough java.sql. :)

Well, yeah, that's exactly what it is :-)

But it's reasonably common for people to need to write this sort of l33tc0de even when they're using JPA for most things.

Also EntityManager has unwrap for years

So typically we use unwrap() to do things which are not portable between providers.

My goal here is to let people do hardcoded JDBC calls in a way which is portable. And for that unwrap kinda lacks type-safety.

And so you might ask, well, why didn't I just propose adding a getConnection() method?

And the answer is that that actually doesn't work very well or very portably because it interferes with eager releasing of connections, etc. It's better to pass the Connection to a lambda, since the provider can then release the connection according to whatever connection-management strategies it's using internally.

So let's make all those users happy.

👍

Copy link
Contributor

@lukasj lukasj left a comment

Choose a reason for hiding this comment

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

LGTM

@lukasj lukasj merged commit 41c2131 into jakartaee:master Aug 21, 2023
4 checks passed
@gavinking gavinking mentioned this pull request Aug 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

easy access to the JDBC connection associated with the EM
3 participants