Spring nested @Transactional method and rollback

I have a @Service class that has an @Transactional method that calls another @Transactional method in the same class. I tested the rollback behavior for this, and I found that it was not working correctly. The code looks something like this:

@Service public class DefaulService implements ervice { @Transactional public void methodOne() { methodTwo(); //question edited //this seems to be the problem this.serviceDAO.executeUpdateOperation(); //test rollback throw new RuntimeException(); } @Transactional public void methodTwo() { //DAO stuff } } 

After running the One method, I check the database and the changes there, although "JDBCTransaction - rollback" is displayed in the log.

If I call the Two method individually and add an exception at the end of it, the changes will be discarded correctly.

Is there a way to make the One methods the correct rollback changes that occurred during a @Transactional nested call? I had the impression that the default distribution of REQUIRED would achieve this, but it does not seem to work. Thanks

UPDATE

Ok, I just noticed something else. Right before throwing an exception, I call dao services and perform a manual update through 'executeUpdate'. If I comment on this line, nested rollback works. So it seems that the problem is actually causing the DAO and triggering an executeUpdate request. But shouldn't this also be done inside the current transaction?

+7
source share
1 answer

You certainly get an "ervice" instance from the bean factory when calling methods, right? The bean factory needs to set up a proxy server that implements transactional logic around each method call. I got the impression that this only works when "outsiders" call methods through a proxy server and do not necessarily work when one method calls another, because this method is a direct call inside the implementation object and does not go through the AOP proxy.

+1
source

All Articles