r/java • u/Mixon1212 • Nov 01 '26
Hibernate lost my update
https://bartoszosipiuk.pl/en/blog/hibernate-lost-my-update/Hibernate silently commits part of a transaction if Spring Boot (or generally any async operation) shuts down mid-transaction.
In my past experience I found multiple bugs in different libraries, but this is the first time I though it needs a separate blog post. Losing part of the updates is one of the worst things that can happen (breaking business rules, and making debugging issues extremely hard).
Hope you find this useful and use the fix. FYI: it's already tested in prod with +80k/t(ransactions)ps.
69
Upvotes
26
u/configloader Nov 02 '26
Dont use JPA
20
u/qmunke Nov 02 '26
While I'd agree this behaviour is definitely worth the report as it's not obvious, your actual issue seems to be your database schema allows you to insert data in an inconsistent state. You can't rely on Hibernate to enforce your database schema restrictions.
6
u/zx-cv Nov 02 '26
I would definitively classify this as a bug. Under no circumstances should Hibernate skip flushing and attempt to commit - this breaks the unit of work.
4
u/766cf0ef-c5f9-4f4f Nov 02 '26
What are these partial transactions? Am I misunderstanding something?
16
u/maxandersen Nov 03 '26
Maybe Iām missing something but your standalone code does exactly what docs says you shouldnāt. Donāt use the sessions after you closed session factory.
Sounds like you expect hibernate to be multi thread safe. Itās not. You and/or spring in this case is expected to not mix up concurrent access.
Even raw jdbc would have issues here if you did similar.
3
u/stefanos-ak Nov 02 '26 edited Nov 02 '26
edit: ignore me. I just read the actual Hibernate report. God what an awful article in comparison to the Hibernate report... Just delete the article and link to the Hibernate report... šµ
sounds to me that you have just disabled OSIV. Which, good for you, but it's not Spring's default. (so, you know, you are holding the hot potato now).
Also, to my knowledge you are not supposed to close a session with open transactions. Which is one of the things that spring OSIV takes care for you.
11
u/definitelyoneof Nov 02 '26
Another day, another reason to stay away from ORMs
1
u/sgrinovero 25d ago
You're essentially shutting down Hibernate, mid-flight of some operations, and then you instruct the TransactionManager to go ahead and commit like everything is fine?
3
u/Paw565 Nov 02 '26
Poland mentioned šµš± š¦
1
u/n4jgg 22d ago
Still seeing people use hibernate is the real pain in this post
1
u/fykup 20d ago
Hibernate lost me a long time ago. In 2026, I'd much rather use explicit SQL with Jdbi or jOOQ than introduce a full ORM.
The memory overhead can be substantial, too. In a recent comparison using largely shared application infrastructure, my Jdbi implementation used 37% less RAM than direct Hibernate.
And with AI assisting with SQL and mapping boilerplate, I think the productivity argument for ORM is getting weaker. Explicit, understandable code feels like a better tradeoff for many applications.
-1
u/gjosifov Nov 03 '26
one of those bad coding patterns when using Hibernate
read the data from DB
update in memory
wait for the EntityManager to flush the data and to calculate the difference in order to generate Update
this bad pattern generates 2 SQL queries
1 Select and 1 Update
You already have the ID in memory and you already know the operation needs to be an update statement
so use CriteriaUpdate or JPQL Update statement
it is just 1 SQL instead of 2
this also means less time spent in memory, reducing the probability of bad things happening
-2
u/SpaceToaster Nov 02 '26
Uhhh thatās on the database. Properly configured and using a ārealā transaction that should be impossible. So hibernate is making you think something is a transaction that isnāt or something is misconfigured.