jakartaee / persistence

https://jakartaee.github.io/persistence/
Other
191 stars 55 forks source link

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

Closed gavinking closed 11 months ago

gavinking commented 12 months ago

see #432

Tomas-Kraus commented 11 months ago

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 commented 11 months ago

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.

👍