Spring data jpa @transactional

2020-01-31 07:11发布

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 ?

1条回答
Bombasti
2楼-- · 2020-01-31 07:49

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.

查看更多
登录 后发表回答