lives
lives

Reputation: 1185

Spring data jpa @transactional

Below is the flow of my application

Controller - services - repository

At the service layer we have @Transactional annotation We also have jpa:repository configuration where we specify the entity manager and txn manager.

My doubt is I feel that the txn manager specified in jpa:repositories is considered and there is no impact of specifying @Transactional at service layer. For eg : the service layer @Transactional can be mapped to custom txn manager where as the repository invoked by the service may have a different txn manager. In that case won't it create a problem?

Can Somebody clarify do we ever need to put @Transactional at service layer when we are using jpa repository ?

Upvotes: 12

Views: 17947

Answers (1)

Alan Hay
Alan Hay

Reputation: 23226

See section 2.3 of the Spring Data Reference:

http://docs.spring.io/spring-data/jpa/docs/1.0.0.M1/reference/html/#transactions

CRUD methods on your repository are transactional by default. While these transactions can be configured as required, it is normally the case, as suggested in the comments above, that transactions be specified at the Service layer and in that case:

The transaction configuration at the repositories will be neglected then as the outer transaction configuration determines the actual one used.

So, in answer to your question, transactions can (and should be) specified at the service level regardless of any Spring Data transaction management.

Upvotes: 28

Related Questions