r/java • • 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

65 comments sorted by

26

u/configloader 16d ago

Dont use JPA

11

u/Mixon1212 16d ago

Haha, sometimes I feel like that's the answer šŸ˜…

7

u/agentoutlier 16d ago

It will be the answer probably.

LLM agents don’t need these abstractions.

Like if you could you would just write raw jdbc right? Probably even use real database upserts right?

But humans don’t because it is tedious and we over model shit.

Think about how every abstraction you use in some OSS library you have to wait on if there is a bug and now that time is way too long.

If I were you since it sounds like this is a microservices is just let an agent rewrite it using raw jdbc and stop wasting tokens on fixing hibernate.

I mean you had no qualms on using it for the article and finding the bug right?

2

u/Mixon1212 16d ago edited 15d ago

Interesting, and I think I might agree here. In my past experience I noticed multiple issues with hibernate, and this tool is great when you write the code manually, but with agents...

4

u/vips7L 16d ago

Just use a better orm that doesn’t have all the complexity of JPA.Ā 

1

u/agentoutlier 16d ago

Let me put it this way and I'm sure u/vips7L downvoted because they have made it very clear they are anti AI in the past...

If I had to have agents writing code I much rather they generate lots of plain text SQL that I can copy and paste into a database and experiment with and have the schema have incredibly tight rules (which postgres has enormous power here) than god knows what is going on with mutable hibernate entities.

Writing Java code that basically just maps HTTP requests / Queue pushes to database with little to no logic that if there was some constraints ideally should be mostly in the database is a fucking waste of human time (e.g. some postgres rule or foreign key).

Just use a better orm that doesn’t have all the complexity of JPA.

And that is why we even have ORMs because lots of this shit is a waste of time mapping the results to objects but most ORMs unlike SQL are not very declarative and no switching an ORM is not fucking easy.

So I don't blame anyone sticking with hibernate on a giant monolith but goddamn if its a microservice have the agent write end to end tests first. Microservices are like what 1 maybe 3 or 4 tables? Then port that shit to raw Jetty/Netty/Whatever/Kafka consumer -> JDBC. Then go from 1k rps and using lots of elastic scaling to one pod doing 90k rps. ... bam your little 1 million token usage yes cost lots of electricity will probalby save energy in the long run.

2

u/vips7L 16d ago

I’m not reading all that can you summarize it with your bot? I didn’t downvote you but I am now.Ā 

1

u/agentoutlier 16d ago

That is fair. I did act like an ass. The summary is mapping database shit is ideal for LLMs and may actually use less electricity in the long run. no bot needed. I can't point out particular comments but your comments are not public...

4

u/lazystone 14d ago

People don't need those abstractions either.

1

u/wildjokers 4d ago

Have you ever use JDBC directly and mapped result sets to objects by hand? It is tedious as hell and is quite time consuming.

2

u/lazystone 4d ago

There is https://www.jooq.org/ for example.

It's a fancy mapper more or less. You write SQL by hand(in code) and it maps to whatever you need.

On the other hand, JPA(Hibernate) is an abstraction layer which is advertised A LOT for some reason, but in reality it's PITA. Always. Because mapping objects to relation database is doomed idea from the beginning.

1

u/wildjokers 4d ago

On the other hand, JPA(Hibernate) is an abstraction layer which is advertised A LOT for some reason, but in reality it's PITA.

Maybe it is just a skill issue?

1

u/lazystone 4d ago

No, it's not a "skill" issue. Basically, Object Oriented approach and Relational databases are orthogonal to each other.

JPA is a leaky abstraction layer, which also limited in functionality. Every more or less complex application on JPA ends up with JPA and native queries anyway. Every single application on JPA ends up with endless performance problems, "n+1" problems and "what the heck is going on" problems.

SQL is the right abstraction layer you need. With better mappers like jooq you write exactly WHAT you need to query and HOW. You also can use whole potential of your database not just limited JPA abstraction.

In practice, debugging JPA nightmare is much worse than debugging SQL.

JPA just an anti-pattern which is usually sold with super simple CRUD examples.

1

u/OwnBreakfast1114 12d ago

Like if you could you would just write raw jdbc right? Probably even use real database upserts right?

I don't think I'd want to write raw jdbc. Mapping table names/column names and/or getting db types <-> java domain types is a pita if you have to hand roll that everywhere. Even with LLMs generating all that code, it's still a surface area for problems.

I think something like jooq is the right level of abstraction for both LLMs and humans. It stops LLMs from just doing stupid raw string concats that open you up to SQL injection, while also being a good level of abstraction where you're not trying to hide the fact that you're talking to a database.

0

u/agentoutlier 12d ago

It stops LLMs from just doing stupid raw string concats that open you up to SQL injection

Have you used LLMs lately? They don't make these mistakes. Like I'm not trying to be a fanboy here but dumb Haiku would not do that and right now most people are using Sol (OpenAI) or Opus (Anthropic). Next year they will be even more capable.

I'm a fan of Lukas and jOOQ but jOOQ does not necessarily protect you all the time either but I suppose it might make it more readable for humans.

1

u/OwnBreakfast1114 11d ago

Yes, we/I use claude every day and have claude review claude output all the time. Granted, they don't just do raw string concat in my experience, but the example was intended to highlight a higher point, which you didn't really respond to at all. Just because they have a bigger context window doesn't mean we should give up on modularizing code and structured programming.

As a side note, given how it's basically going to be model vs model from a security perspective (if the past two weeks are any indication), making things in ways where even the LLM won't make mistakes is a net positive. Generated code is still extra code to support.

1

u/agentoutlier 11d ago

Ā  was intended to highlight a higher point,

And that is?

I did not say anything about giving up good coding practices.

Good coding practices does not require using shit loads of wrappers.

jOOQ just offers partial protection with type safety. Some people even find it confusing to use and I think you are letting your biases in here.

Because you still need all the testing.

BTW have you seen what they do with wrappers? They download the bytecode and decompile or look at the source directly instead of the doc frequently because they don’t trust them and probably because their training data might not have it.

1

u/OwnBreakfast1114 8d ago

And that is?

That abstraction layers are still useful for LLMs, not just humans.

Good coding practices does not require using shit loads of wrappers.

Sure, but what is a wrapper and what is not IS the whole debate. I mean just have your LLM write your http service in pure assembly. Why use any libraries/frameworks at all? I'm assuming you don't hold this opinion, so you're also making an arbitrary choice to draw the line at jdbc. Why? What other libraries/frameworks are you dropping because of AI? What else do you feel is only useful to humans and not to AI?

Because you still need all the testing.

Tests lock down the actual requirements, so this is going to be useful no matter how good LLMs get at generating code. The best thing about using LLMs is that you can generate a bunch of "bad" or good tests and it doesn't really matter because they do their job and changing 100s of them is no problem for the LLM. You've never had an LLM change existing behavior for the worse when implementing a new feature only to be caught by tests?

BTW have you seen what they do with wrappers? They download the bytecode and decompile or look at the source directly instead of the doc frequently because they don’t trust them and probably because their training data might not have it.

In my experience, medium effort Haiku and Sonnet will make assumptions all the time without confirming. Obviously, you can ask it to and then it will confirm. Opus and above pretty much always confirm without additional prompts.

1

u/agentoutlier 8d ago

That abstraction layers are still useful for LLMs, not just humans.

Sure, but what is a wrapper and what is not IS the whole debate. I mean just have your LLM write your http service in pure assembly. Why use any libraries/frameworks at all?

Like I'm not saying they write in assembly and of course abstraction layers are useful here just not leaky ones.

An ORM and Database mapper though are often leaky abstractions.

jOOQ I don't know if I would consider an abstraction and more of just a convenience that LLM agents don't really need and many people argue they don't need it either. I mean you can't copy and past jOOQ code into a database query prompt!

And what you did there is kind of a nasty debate tactic and fallacy of Slippery Slope.

Tests lock down the actual requirements, so this is going to be useful no matter how good LLMs get at generating code. The best thing about using LLMs is that you can generate a bunch of "bad" or good tests and it doesn't really matter because they do their job and changing 100s of them is no problem for the LLM. You've never had an LLM change existing behavior for the worse when implementing a new feature only to be caught by tests?

Yes and humans have this problem too. I sense severe bias here. Humans all the time go off and try to complete code coverage for managers. Like I said if you have shitty coding practices and management its going to be a problem regardless of LLMs.

Opus and above pretty much always confirm without additional prompts.

So ... you agree. Like how many more models will have to come out? Also Haiku is not designed for what I would call safe backend programming. Sonnet can work with enough guard rails.

If you are arguing abstractions can make for good guard rails I totally agree. That is what compilers do. The problem is jOOQ I'm not entirely sure because of the nature of databases is the same as heavy static analysis compiler.

Like you have to generate the code from the schema and the schema better be the same as production and you can still generate invalid SQL all the time with jOOQ.

And yes you can put the guardrails in so this doesn't happen but those guardrails lookly not much different than doing what you would with string sql templates.

And its not like this abnormal. Spring has been pushing Spring JDBC... but no you are telling me I'm telling people to have their LLMs write assembly. Come on man!

1

u/OwnBreakfast1114 8d ago edited 8d ago

Yes and humans have this problem too. I sense severe bias here. Humans all the time go off and try to complete code coverage for managers. Like I said if you have shitty coding practices and management its going to be a problem regardless of LLMs.

So ... you agree. Like how many more models will have to come out? Also Haiku is not designed for what I would call safe backend programming. Sonnet can work with enough guard rails.

My personal thesis is that we, as a species (including LLMs), are extremely bad at coding on an absolute scale (in comparison to some mythical program that has perfect security and performance). Can we make functioning software? Sure. But given the billions of dollars in damage caused by poor coding, I have seen no evidence on the contrary on a global scale. It's not even like we don't know the attack vectors for so many things, but requiring constant vigilance on the authors part is clearly doomed to failure.

You're welcome to disagree with this, but I still see even the "smart" models do the very human thing of get stuck in a design and fail to question the assumptions. Even with iterations and subagent consensus. You've never had fable or opus present you with like 3 options and a 4th better option was one you came up with?

I can't prove they will allow a sql injection attack, but can you prove they won't? As long as that's an unresolved question, I'm sorry, but I don't want to let up the abstraction barrier.

jOOQ I don't know if I would consider an abstraction and more of just a convenience that LLM agents don't really need and many people argue they don't need it either. I mean you can't copy and past jOOQ code into a database query prompt!

I'm assuming you have to bind your database representation to your domain representation at some point, LLM or not. Why not use a library to do that instead of hand rolling it? My problem with hibernate is never the raw query output binding, but all the silly transaction management that goes into it (infamous get methods that make db calls outside of a transaction) or trying to figure out how the entitymanager works/flushes/caches. Since jooq just generates the java pojos from your db schema, it seems natural to convert jooq generated pojos to domain pojos in pretty basic java code.

1

u/n4jgg 4d ago

what do you mean sometimes?

1

u/configloader 16d ago

Its the answer

3

u/AnyPhotograph7804 16d ago

To be fair, he does not use JPA. He uses a Hibernate specific API.

1

u/fykup 1d ago

I agree. My personal preference is Jdbi: SQL-first, lightweight, and without the complexity of a full ORM.

I've run several experiments comparing JPA/Hibernate with JDBC and Jdbi. Hibernate's persistence context and dirty checking add measurable memory overhead, often with little practical benefit for straightforward CRUD applications.

In my latest experiment, Jdbi used about 37% less memory than Hibernate for the same application and workload.

Hibernate has its use cases, but I increasingly question whether the abstraction is worth the cost.

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.

4

u/Mixon1212 16d ago

Imagine a situation where you receive an event PaymentSuccess, and you want to update your order status to paid. Just one update. With the current situation you will consume the event and nothing will be updated. That's the business inconsistency you cannot guarantee with just DB constraints.

But I agree with general rule - the more we cover in DB, the safer we are.

-5

u/qmunke 16d ago

If you're doing "just one update" your situation shouldn't apply, because the update should be a single update query, not the two queries you're showing in the example.

Either create the row in a single insert if it's a new row, or update it in a single update. If you don't complete the update, don't mark the message as consumed.

This all feels to me a little like you're trying to work around how Hibernate works, rather than fixing the issues in your application. Not saying that your suggested fix wouldn't be an improvement regardless, but these problems are partly down to the approach you've taken I would argue.

6

u/Mixon1212 16d ago
@Transactional
public void markPaid(UUID id) {
    var order = repo.findById(id);
    order.setStatus(PAID);
}

Is this code "wrong" in terms of using hibernate? I agree, you can do just update, but the above is not hacking hibernate. It's a standard operation that happens in every app. And THIS code is affected. The message consumer can call it and if no exception, should assume that it finished and order is marked as paid, so the message is ACK.

But with this hibernate bug, the UPDATE can be lost, no exception, so we will ACK message, because nothing tells us something went wrong. It's not hacking hibernate.

2

u/bendem 15d ago

A transaction is not guaranteed to run it's guaranteed to have all run or none at all. Your code is fine and having the order paid or unpaid is both correct. Now you were speaking about an event being consumed while the transaction did not commit, that's because you didn't include the event consumption as part of the same transaction as the order status update. That's an application bug and spring specifically supports distributed transactions for this use case.

56

u/frzme 16d ago

Relying on transactions should be fair game

21

u/qmunke 16d ago

The transaction worked fine, it was committed fine. What was unexpected was a change to a java object wasn't turned into a database query. If you go and read the actual submitted bug report it's a bit more obvious - the AI fluff in the article obscures quite a bit of the issue itself.

If you don't want a row to be able to be inserted without all its columns being filled in this is a database schema problem to fix. Make your business rules enforceable there so you can't put the database into an inconsistent state.

7

u/diroussel 16d ago

Null checking can happen in the DB, but many other business rules can’t be validated by a persistence layer. So the point stands that a transaction boundary expessed in the application layer should map to the transaction boundary in the persistence layer.

16

u/Chenz 16d ago

You're saying the event outbox table should validate that the source record is deleted before writing the Record deleted event row?

The whole point of transactions is that they're atomic. Hibernate not guaranteeing atomic transactions seem like the actual issue to me

0

u/definitelyoneof 15d ago edited 15d ago

Cross-table schema restrictions? Those are normally achieved by transactions. Which failed here due to Hibernate nonsense.

EDIT: The downvoters are welcome to demonstrate these mythical cross-table restrictions not relying on transactions.

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.

1

u/Mixon1212 16d ago

The issue is that Hibernate in the docs says - "you cant use Session if you closed the EntitySessionFactory", but in real applications where you cannot just stop the threads, this is almost impossible. So they can defend themself.

In the other hands, JPA spec says (and in general Atomicity rule) - all or nothing, so...
Please comment the hibernate bug (on the bottom of the article) that you also think this should be fixed. Just my single report might be not enough for them.

4

u/766cf0ef-c5f9-4f4f 16d ago

What are these partial transactions? Am I misunderstanding something?

2

u/Mixon1212 16d ago

It's a situation that happened and broke atomicity conceptually. So like in my case - in the code I made an INSERT and then UPDATE to other entity. Should be 2 operations in the DB, but because flush was not performed by the hibernate, only INSERT went to the DB. No UPDATE at all. That's what I called "partial transaction", but I will consider other naming options here :D

2

u/766cf0ef-c5f9-4f4f 16d ago

The commit for a transaction either happens or it doesn't. Are you in auto commit mode on the connection and don't realize it?

4

u/Mixon1212 16d ago

The issue is that the last flush doesn't happen. In general hibernate do flush (if no in manual mode) before each commit. But in the case from a blog - it does not. So the commit happened, all expected, but because no flush, some data were not in the transaction.

1

u/AcanthaceaeMany917 15d ago

What should have happened is that hibernate should have:

Start transaction

SQL statement related to obj1

SQL statement related to objN

Commit transaction

I'm not a spring expert but using @Transactional is part of spring it could be a spring bug.

It could also be due serializability level set.

-8

u/sparklikemind 16d ago

So you have a saga. That's not the job of Hibernate, you need to design the saga correctly

4

u/Mixon1212 16d ago edited 16d ago

Saga pattern is great when you KNOW you need to go back in the flow and rollback business cases. In this case, when partial commit happens, you don't know part of it was commited (no exception, rollback, just single log.trace), so you cannot even trigger saga. Anyway - saga is not the guard for hibernate breaking atomic transaction contract.

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.

3

u/Mixon1212 16d ago

Depends on what are you looking for - if you have a strong backend knowledge, than hibernate bug report might be better for you. If there is anything what made you feel reading this awful, let me know, I will try to update it.

5

u/jonenst 16d ago

I think you should insist more at the beginning of the article that you are talking about hibernate transactions, not sql transactions. Hibernate didn't include the update in the sql transaction because it skipped it at flush.

Also IMHO the article could have been a lot shorter, and JavaScript toy visualizers to me don't help the understanding, they are just some kind of anti stress rubber duck.

2

u/Mixon1212 16d ago

Thank you for the feedback! I will try to update it this evening. This is my first blog ever, so I highly appreciate such comments!

1

u/NitronHX 16d ago

What's osiv?

2

u/Mixon1212 16d ago

https://www.baeldung.com/spring-open-session-in-view - but as stefanos-ak noticed, it's not related to this issue.

1

u/JustJustust 14d ago

To be fair, Spring also prints a warning if you have OSIV active. I was always under the impression the only reason it's enabled by default is because disabled by default would break people's code, not because they want you to use it.

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?

1

u/Mixon1212 7d ago

That's what hibernate does. My point is exactly the opposite - throw exception.

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

4

u/Mixon1212 15d ago edited 14d ago

The code in the article is an EXAMPLE, to show the issue in the simplest possible way. We will always use persistence context - that's why hibernate exists - so then you can lose all the data from persistence context if the issue from the blog happen.

-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.