4

I'm using Spring 2.5 transaction management and I have the following set-up:

Bean1

@Transactional(noRollbackFor = { Exception.class })
public void execute() {
  try {
    bean2.execute();
  } catch (Exception e) {
    // persist failure in database (so the transaction shouldn't fail)
    // the exception is not re-thrown
  }
}

Bean2

@Transactional
public void execute() {
  // do something which throws a RuntimeException
}

The failure is never persisted into DB from Bean1 because the whole transaction is rolled back.

I don't want to add noRollbackFor in Bean2 because it's used in a lot of places which don't have logic to handle runtime exceptions properly.

Is there a way to avoid my transaction to be rolled back only when Bean2.execute() is called from Bean1?

Otherwise, I guess my best option is to persist my failure within a new transaction? Anything else clean I can do?

Damien
  • 2,254
  • 1
  • 22
  • 30
  • I'm not seeing why it's rolling back. Are you re-throwing the exception in the catch block? – skaffman Oct 02 '10 at 10:23
  • No, I don't. It seems it's rolling back because Spring TransactionInterceptor marks the transaction for rollback when exiting Bean2. – Damien Oct 02 '10 at 18:47
  • I solved my problem by persisting the failure within a new transaction, using REQUIRES_NEW propagation. But I'm curious to know if there was a better solution. – Damien Oct 07 '10 at 14:36

1 Answers1

1

This is one of the caveats of annotations... your class is not reusable!

If you'd configure your transactions in the XML, if would have been possible.

Assuming you use XML configuration: if it's not consuming expensive resources, you can create another instance of bean2 for the use of the code you specified. That is, you can configure one been as you specified above, and one with no roll back for exception.

Eran Harel
  • 2,325
  • 19
  • 28
  • Thanks, that's a good remark. Anyway, the whole set-up of transactions in my application was very bad, with transactions declared at the DAO level. Because of that I still have some embedded transactions like shown in the question but I'll try to refactor when I get a chance. – Damien Oct 22 '10 at 23:04