Aravind A
Aravind A

Reputation: 9697

Autocommit and Spring Declarative Transactions

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.

Upvotes: 0

Views: 3764

Answers (3)

kem
kem

Reputation: 1127

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.

Upvotes: 0

Tom Anderson
Tom Anderson

Reputation: 47243

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.

Upvotes: 0

Pankaj Pushp
Pankaj Pushp

Reputation: 11

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

Upvotes: 1

Related Questions