I saw a report that shutdown during an asynchronous operation can leave a Hibernate transaction with only some intended changes in the database. How can that happen, and what should an application do about it?
Can shutdown during an async transaction leave only some Hibernate updates committed?
Answers
The distinction is between the database transaction and the work the application intended to include in it. In the reported case, an insert reached the database, but a change to another entity was not flushed before commit. The database could commit the SQL it received atomically while the application's intended unit of work was incomplete.
That doesn't make the outcome acceptable for the application. If Hibernate skips a required flush and still attempts to commit, the result can violate business expectations even though the database did not commit half of a SQL transaction.
Reply to this answer
Look closely at the shutdown order. Async tasks may still be using a Hibernate session when the application starts closing the session factory. A safer design is to stop accepting new work, wait for active tasks and their transactions to finish, and only then close the resources they use.
Also test the timeout path. If the application cannot wait indefinitely, decide how unfinished work is stopped and rolled back rather than assuming shutdown will finish it cleanly. Closing a session factory while sessions are still in use may be outside Hibernate's supported lifecycle.
c.746!plivora:
A shutdown test that pauses a task between its database operations could help expose the ordering problem. Check the final database state, not just whether the process exits.
Reply to this answer
Use database constraints for invariants the schema can express. They're a useful backstop, but they don't replace transaction handling. For example, a database constraint may not be able to tell that an incoming event was consumed without the intended order status update.
Some people see an inconsistent committed state as a schema problem, while others see a missing flush before commit as the central issue. Those aren't mutually exclusive. Constraints can limit the damage without making an incomplete unit of work correct.
Reply to this answer
I wouldn't assume every async Spring Boot transaction has this problem or apply a fix blindly. The discussion describes a particular shutdown and flush scenario, but it doesn't establish which versions or configurations are affected.
Try to reproduce it with your Hibernate and Spring versions, including the actual shutdown sequence. Check what SQL was sent, whether a flush occurred, and whether the transaction committed or rolled back. Then check the relevant Hibernate issue and documentation for the status and any version-specific fix.
Reply to this answer
Add your answer
Answers are reviewed before publication.
_x72!veltrano.4:
So a transaction alone doesn't guarantee that every pending entity change actually made it into that transaction?
6k!moxel_274:
Right. The database can only commit or roll back statements it received. Hibernate also has to flush the pending changes as expected.