[WIP] Require transactions by default for all Hibernate ORM session use#50019
[WIP] Require transactions by default for all Hibernate ORM session use#50019yrodiere wants to merge 3 commits intoquarkusio:mainfrom
Conversation
|
Thanks for your pull request! Your pull request does not follow our editorial rules. Could you have a look?
This message is automatically generated by a bot. |
|
/cc @gsmet (hibernate-orm) |
|
Great idea - tagging @FroMage for awareness |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
This sounds like we should have a BTW, shouldn't we also require transactions for HR, then? Just to be consistent. |
Perhaps yes, but bear in mind in that case the mechanism is quite different as it wouldn't be JTA. We'd need to think of the consequences of that - I'd be especially more concerned on starting them automatically (w/o an explicit opt-in) as we'd not want to give a false sense of safety w/o the user understanding the implications. |
Not sure why... ? Transactions are started lazily, no? Meaning on first connection request, which with Hibernate would happen on first load or flush? In any case, I strongly oppose adding many different annotations without thinking of a unified approach. The current state of things is quite confusing already. |
|
Ah, so So, hold on. In ORM, the session is lazy, and the TX is lazy. They're only opened when used by API. Except, before this patch, we used to be able to access ORM for Read-Only operations. Now, this means we can never use ORM from outside of an endpoint annotated with This is becoming a 2-factor API: you can only use it with an annotation and using its API. I do think we are going to have to talk about simplifying access to the ORM API, even though this can be done later, but perhaps REST endpoints by default should have an automatic |
Technically you can also start a transaction programmatically with
That would cause a lot of backwards incompatibility since it would effectively extend the scope of We could instead have some sort of "last-minute" default transaction scoping that, assuming a request scope exists and no transaction scope exists when first accessing the But:
|
Yeah, because Well, the current PR is also backwards incompatible 🤷
They use
This is essentially the default for HR and REST, no? If you don't specify a transaction/session, one will be provided for you at the request scope. Seems fairly straightforward and helpful to me.
Except you need to do it on every REST resource, and it requires the user to first get an exception before acting on it. This is boilerplate. You're thinking about this with your expert hat, not your newbie hat. Which is good, it's useful, but you need both hats on :) Anyway, I think this is mostly off-topic for this particular PR, sorry to have diverged on the topic. |
|
@yrodiere What's the status on this one? |
Many tests failing, as you can see. @Sanne suggested we'd better be damn sure use all use cases work fine with transactions before we do this, which I agree with. There are still edge cases where not using a transaction is... arguably correct. So, we'll need to address a good chunk of #51243 before we flip the switch here. |
08ec626 to
8554216
Compare
…rios Split CDI-based persistence unit tests into RequestScopeEnabled and RequestScopeDisabled variants to explicitly test both behaviors: - RequestScopeEnabled: reads work without transaction, writes require transaction - RequestScopeDisabled: both reads and writes require transaction This refactoring prepares for the upcoming change that will make request-scoped disabled by default. The RequestScopeEnabled variants currently assume the default (enabled), while RequestScopeDisabled variants explicitly disable it via config. Deleted original test classes (now replaced by RequestScopeEnabled variants): - MultiplePersistenceUnitsCdiEntityManagerTest - MultiplePersistenceUnitsCdiSessionTest - SinglePersistenceUnitCdiEntityManagerTest - SinglePersistenceUnitCdiSessionTest Deleted redundant tests (covered by new variants): - SessionWithinRequestScopeTest/Disabled (uses same @ActivateRequestContext pattern) - StatelessSessionWithinRequestScopeTest/Disabled (same pattern) - NoTransactionTest (covered by NoRequestNoTransaction tests) Assisted-By: Claude Sonnet 4.5 <[email protected]>
Assisted-By: Claude Sonnet 4.5 <[email protected]>
Assisted-By: Claude Sonnet 4.5 <[email protected]>
8554216 to
3979a20
Compare

Please do not merge, I expect many tests will fail but I want to get a sense of how many.
We will need an entry in the migration guide.
cc @Sanne @gavinking
See also https://hibernate.zulipchat.com/#narrow/channel/132094-hibernate-orm-dev/topic/StatelessSession.20insertMultiple.20.2F.20updateMultiple.20.2F.20etc.2E/near/538862234
By the way, here is the relevant code:
quarkus/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/session/TransactionScopedSession.java
Lines 116 to 133 in e16597b