Shafqat Shafi
Shafqat Shafi

Reputation: 1238

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.

Upvotes: 4

Views: 3886

Answers (1)

Martin
Martin

Reputation: 2245

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.

Upvotes: 4

Related Questions