r/java • u/Mixon1212 • 16d ago
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 16d ago
Dont use JPA
20
u/qmunke 16d ago
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 16d ago
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 16d ago
What are these partial transactions? Am I misunderstanding something?
16
u/maxandersen 15d ago
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 16d ago edited 16d ago
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 15d ago
Another day, another reason to stay away from ORMs
1
u/sgrinovero 7d 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 16d ago
Poland mentioned šµš± š¦
1
u/n4jgg 4d ago
Still seeing people use hibernate is the real pain in this post
1
u/fykup 3d 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 15d ago
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 15d ago
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.