I have got stuck a number of times asking myself why wasn't my code persisting the data in the database. I mean, I have got my simple method annotated with @Transactional
and everyting seems to go fine, no problems. After trying to discover the reason for some time I will eventually remember that I am actually calling my transactional method from the inside of the same class, and hence spring will promptly ignore the annotation. It happend to me that in those cases I was marking the method as transactional on my @Controller
layer, which seemed to limit code re-usability. So, I have two questions in regard of this problem:
- On which level of the spring architecture is the
@Transactional
best placed (@Controller
,@Service
etc..)? - Why exactly does spring ignore the annotation when the annotated method is called from the inside of the class?