Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top