What is the difference in using this @Transactional
annotation in Domain/Service layer and Dao layer. Will it provide any advantage using in Domain layer.

- 12,010
- 6
- 65
- 78

- 9
- 5
3 Answers
It is good practice to use @Transactional
in the service layer because it governs the logic needed to identify the scope of a database and/or business transaction. The persistence layer by design doesn't know the scope of a transaction.
DAOs can be made @Transactional
like any other bean, but it's a common practice to use it in service layer. We tend to do this because we want separation of concerns. The persistence layer just retrieve / stores data back and forth from the database.
For example, if we want to transfer some amount from one account to another, we need two operations, one account needs to be debited other needs to be credited. So, the scope of these operation is only known by service layer and not persistence layer.
The persistence layer cannot know what transaction it's in, take for example a method person.updateUsername()
. Should it run in it's own separate transaction always? there is no way to know, it depends on the business logic calling it.
Here a few thread you should read

- 8,740
- 6
- 40
- 61
The place you use @Transactional
annotation specifies the scope of your transaction.
When using it in DAO layer you are specifying that each DAO operation will be committed in a transaction.
When using it in service you are specifying that each service operation (business unit) is committed in a transaction, this is recommended since usually service method represents a business unit which should be included in the same transaction since any failure should roll back the whole business unit.

- 6,412
- 3
- 20
- 43
-
Thanks for your response . But i am using this annotation for Roll back. i want clear understanding on usage of this annotation in Domain and Dao layers. i also wanted to know advantages and disadvantages of using @Transactional annotation – Vamsi Krishna Jul 25 '17 at 14:35
@Transactional: There are two separate concepts to consider, each with it's own scope and life cycle:persistence context , database transaction It look like u r more into database traction :
@Transactional annotation itself defines the scope of a single database transaction. The database transaction happens inside the scope of a persistence context.
The persistence context is in JPA the EntityManager, implemented internally using an Hibernate Session

- 11
- 2