Autocommit and Spring Declarative Transactions
I have a single service method annotated with Propagation.Required
. It performs three separate operations .
- Insert to table 1 from table z if no records are in table 1
- Insert/Update table 1 as per user edits/Additions
- Delete x records from table 1
Forgive my ignorance but shouldn't all these run under a single transaction ? In the sense, if the third Query runs into an Exception, shouldn't the first & second rollback too ? This开发者_如何学运维 doesn't happen in my case. Will hibernate auto commit setting affect the txn boundaries in any way ? Auto commit is set to true in my case. What I require is the commit should take place in any of these tables only if all are successful.
could you try to add one more layer higher than service layer and start transaction from there.
Yes, the Hibernate connection.autocommit
property setting will affect transaction boundaries.
If you set this to true
, Hibernate will put the underlying JDBC connection in autocommit mode, which will wrap each statement you execute in its own database transaction.
So, for example, if your third query/statement fails, only your third query/statement will get rolled back.
To execute all three as a single unit, you need to have autocommit off and execute all three in the context of a single transaction, declarative or otherwise.
You definitely don't want autocommit on. That will probably commit after every operation. Switch autocommit off, and add an explicit commit at the end.
精彩评论