I have a DAO class where I have 2 methods annotated with the Spring @Transactional
annotations, as follows:
public class ClassDAO {
@Transactional
public void save() throws Exception {
}
@Transactional
public void save2() {
}
}
I want those 2 methods to be part of the same transaction so if any method fails, the whole transaction is rolled back.
These methods are being called from the service layer, as follows: @Autowired private ClassDAO dao;
@Transactional
public void processDAO() {
dao.save();
dao.save2();
}
I'm using @Transactional
annotations on service layer since I've read this is the best approach, but on the other hand I've read @Transactional
attribute works only when calling an annotated method on a reference obtained from applicationContext, so if this is true, that would explain why the @Transactional
annotation is not working on the service layer.
I have one questions:
If @Transactiona
l attribute works only when calling an annotated method on a reference obtained from applicationContext, then how is it that it is a good practice to place Transactional annotation on service layer?
Thanks and best regards.