문제

I have a single service method annotated with Propagation.Required. It performs three separate operations .

  1. Insert to table 1 from table z if no records are in table 1
  2. Insert/Update table 1 as per user edits/Additions
  3. 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.

도움이 되었습니까?

해결책

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.

다른 팁

could you try to add one more layer higher than service layer and start transaction from there.

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top