Spring 2.5 - Rolling back first method, when the second method throws Runtime ex
Joined: Jul 21, 2003
Trying to rollback the previous write to database using Spring transaction for POC and learning purposes.
There are two functions that are part of the same transaction namely: insert and then update. The insert successfully writes to database using Hibernate 3.2. I throw a Runtime exception from update on purpose to exercise rollback of insert. Even though update throws the Runtime exception, the data inserted by insert method stays committed in database and does not get rolled back.
org.poc.tx.Manager class has insert and update methods.
Wow, talk about waking the dead, that is almost a year old.
The answer is that he is not in a single transaction there. He has two transactions, one for each method, so the insert call started a transaction, completed and committed the transaction. Then the call to update creates a new transaction.
It is all because a call to manager.method is what is being wrapped by AOP. So if in the insert method it calls the update method directly, then it would all be in one transaction.
Can I invoke them separately and still make them participate in the same transaction?
No you can't, because the scope of your transaction is in the manager class.
What you can do is make another method in your manager class to do your business logic (insert-then-update), then call this method in your main class
you can make your main class (class that calls insert then update) transactional. in this case insert-then-update is called in one method in your main class.
Its better practice to use the first approach.
SCJA, SCJP, SCJD, SCMAD, SCWCD, SCBCD, SCDJWS, SCEA, SOA, Spring Certified, OCP MySQL 5 Developer, PMP
Based on your explanation of not being in a single transaction, I had changed the code and put calls to two add methods in one method and made that method Transactional.
Please note that the posted JUnitTest case works fine and the first call to smartBaseBO.add is rolled back when the second call throws exception, but the same code is unable to roll back when used in an application. Thanks for helping me out with this.
Hibernate 3.3.1 GA
Joined: Jan 08, 2004
Looks like the above code works fine when the log level for the hibernate package is set to DEBUG. When I set it to INFO, it doesnt work. Its a weird issue and I have no clue why this is working this way. I would be thankful, if someone can provide some insight into this.