Monday, 15 March 2010

Java Spring @Transactional method not rolling back as expected -


Below is a quick outline of what I am trying to do. I call a method call from two different tables in the database If anything fails, then everything should be rolled back, so if method2 fails, then I need anything that is method1 .

  Public Service Service {MyDAO dao; Public Zero Insertion Collection (Record Collection Collection) {While (collection.size ()> 1) {record 1 record 1 = collection .removeFirst (); Record 2 records2 = archive .removeFirst (); Doubles (record 1, record 2); }} @ Transactions (RollbackFor = Exceptions category, Promotion = Promotions. REQUIRES_NEW) Public Zero again (Record 1 record 1, Record 2 record 2) {method1 (record1); Method2 (record2); } @ Transaction (Promotions = Promotions. REQUIRED) Public Zero Method 1 (Record 1 record) {dao.insertIntoA (records); } @ Transaction (Promotions = Promotions. REQUIRED) Public Zero Method 2 (Record 2 records) {dao.insertIntoB (Record); } Public Zero Set Meadow (Final MyDao Dao) {this.dao = Dao; }}   

Where MyDao Dao is an interface that is mapped to the database to use MyBatis and is set using the Spring Injection.

Now if method2 is unsuccessful, then everything gets pushed into the database from method1 How can I fix this behavior?

Edit:

I have modified the class to give a more accurate description of what I am trying to achieve. If I run doBoth , the roll works back if there is a problem, but if I call doBoth to insertCollection

I got the solution!

Apparently, the internal method for spring transaction methods can not stop the call so I removed the method by calling the transactional method, and put it in a separate class, and rollback only works well is. Below are some examples of fixes.

  Public Sector Fu {Public Zero Insert Collection (Record Collection Collection) {Service MyService = New Service (); While (collection.size ()> 1) {record 1 record1 = archive .removeFirst (); Record 2 record 2 = collection. Review seconds (); MyService.doBoth (record1, record2); }}} Public Class Service {Maida doo; @ Transactional (RollbackFor = Exception.Class, Promotion = Promotion.REQUIRES_NEW) Public Zero again (Record 1 record 1, Record 2 record 2) {method1 (record1); Method2 (record2); } @ Transaction (Promotions = Promotions. REQUIRED) Public Zero Method 1 (Record 1 record) {dao.insertIntoA (records); } @ Transaction (Promotions = Promotions. REQUIRED) Public Zero Method 2 (Record 2 records) {dao.insertIntoB (Record); } Public Zero Set Meadow (Final MyDao Dao) {this.dao = Dao; }}    

No comments:

Post a Comment