dantebarba
dantebarba

Reputation: 1674

Hibernate/Spring - Rollback a transaction within a transaction

Given this example code:

public class MyServiceImpl implements MyService {
    @Transactional
    public void myTransactionalMethod() {
        List<Item> itemList = itemService.findItems();
        for (Item anItem : itemList) {
            try {
                processItem(anItem);
            catch (Exception e) {
                // dont rollback here 
                // rollback just one item
            }     
        }

    }

    @Transactional
    public void processItem(Item anItem) { 
        anItem.setSomething(new Something);
        anItem.applyBehaviour();
        itemService.save(anItem);
    }
}

Here is what I want to achieve:

  1. Only processItem(anItem); should rollback if exception occurs inside it.
  2. If exception occurs, myTransactionalMethod should continue, that means the for-each should end.
  3. If exception occurs inside myTransactionalMethod but not in processItem(anItem), myTransactionalMethod should rollback completely.

Is there a solution that doesn't involve managing transactions manually (without annotations)?.

Edit: I was thinking of using @Transactional(PROPAGATION=REQUIRES_NEW), don't know if it will work within the same bean though.

Upvotes: 4

Views: 1524

Answers (2)

It looks like a case for NESTED transaction. NESTED transaction starts a subtransaction with in the outer transaction with savepoint, allowing it rollback to that savepoint. Since it is a nested transactions they committed at the end of outer transation.

    public class MyServiceImpl implements MyService {
    @Transactional
    public void myTransactionalMethod() {
        List<Item> itemList = itemService.findItems();
        for (Item anItem : itemList) {
            try {
               // If you want to call this method directly configure your transaction use to aspectJ for transaction handling or refactor the code. Refer - [http://stackoverflow.com/questions/3423972/spring-transaction-method-call-by-the-method-within-the-same-class-does-not-wo][1]
                processItem(anItem);
            catch (Exception e) {
                // dont rollback here 
                // rollback just one item
            }     
        }

    }

    @Transactional(PROPAGATION = PROPAGATION.NESTED)
    // Throw some runtime exception to rollback or some checkedException with rollbackFor attribute set in the above annotation
    public void processItem(Item anItem) { 
        anItem.setSomething(new Something);
        anItem.applyBehaviour();
        itemService.save(anItem);
    }
  }

Note that, I have not yet tried this below code see if that helps. You might have to tweak it, if needed. In fact I would love to give this code a try myself sometime soon.

Upvotes: 0

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298818

This is a common misunderstanding. Spring Transactions are implemented through proxies. Proxies are a wrapper around your class. You are accessing the processItem method from the same class, i.e. you don't go through the proxy, so you don't get any transactions. I explained the mechanism in this answer some years ago.

Solution: you need two separate Spring beans if you want nested transactions, both of them must be proxied by @Transactional.

Upvotes: 7

Related Questions