Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to use @Transactional annotation in spring

I'm currently working on a project using Spring boot. I haven't used @Transactional annotation on most of the methods. But sometimes when an error points towards having transactional annotation, then i place the annotation on it. Also i call service methods within same service class.

My Question: Do i have to use @Transactional annotation on every service method.

or what is the best practice to use @Transactional annotation in service methods.

like image 467
Shafqat Shafi Avatar asked Sep 14 '25 17:09

Shafqat Shafi


1 Answers

I personally use @Transactional on every service class (directly on the class, not the methods). It is incredibly useful to avoid having to manage sessions / transactions yourself and i honestly don't see a reason not to have every method use it. I have over 30 entities and associated DAO and service classes using it at the class level and everything works great.

like image 140
Martin Avatar answered Sep 17 '25 07:09

Martin