春季数据 jpa @transactional [英] Spring data jpa @transactional

查看:13
本文介绍了春季数据 jpa @transactional的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的申请流程

控制器 - 服务 - 存储库

Controller - services - repository

在服务层我们有 @Transactional 注释我们还有 jpa:repository 配置,我们在其中指定实体管理器和 txn 管理器.

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

我的怀疑是我觉得jpa:repositories中指定的txn manager被考虑了,在服务层指定@Transactional没有影响.例如:服务层 @Transactional 可以映射到自定义 txn 管理器,因为服务调用的存储库可能有不同的 txn 管理器.那样会不会有问题?

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?

有人可以澄清一下,当我们使用 jpa 存储库时,我们是否需要将 @Transactional 放在服务层?

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

推荐答案

参见 Spring Data Reference 的 2.3 节:

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 方法是事务性的.虽然可以根据需要配置这些事务,但通常情况下,如上面评论中所建议的那样,事务是在服务层指定的,在这种情况下:

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.

因此,在回答您的问题时,可以(并且应该)在服务级别指定事务,而不管 Spring Data 事务管理如何.

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

这篇关于春季数据 jpa @transactional的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆