Alex Rivera | Logout

Where does the @Transactional annotation belong?

Asked 2009-07-03T12:20:11.873
600

Should you place the @Transactional in the DAO classes and/or their methods or is it better to annotate the Service classes that are using the DAO objects? Or does it make sense to annotate both layers?

Edit
Report

1 Answer

108

Transactional Annotations should be placed around all operations that are inseparable.

For example, your call is "change password". That consists of two operations

  1. Change the password.
  2. Audit the change.
  3. Email the client that the password has changed.

So in the above, if the audit fails, then should the password change also fail? If so, then the transaction should be around 1 and 2 (so at the service layer). If the email fails (probably should have some kind of fail safe on this so it won't fail) then should it roll back the change password and the audit?

These are the kind of questions you need to be asking when deciding where to put the @Transactional.

answered 2009-07-07T13:46:46.610

Your Answer