博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
@Transactional注释在哪里?
阅读量:2381 次
发布时间:2019-05-10

本文共 3907 字,大约阅读时间需要 13 分钟。

本文翻译自:

Should you place the @Transactional in the DAO classes and/or their methods or is it better to annotate the Service classes which are calling using the DAO objects? 你应该将@Transactional放在DAO类和/或它们的方法中,还是更好地注释使用DAO对象调用的Service类? Or does it make sense to annotate both "layers"? 或者注释两个“层​​”是否有意义?


#1楼

参考:


#2楼

I think transactions belong on the Service layer. 我认为交易属于服务层。 It's the one that knows about units of work and use cases. 这是了解工作单位和用例的人。 It's the right answer if you have several DAOs injected into a Service that need to work together in a single transaction. 如果您将几个DAO注入到需要在单个事务中协同工作的服务中,那么这是正确的答案。


#3楼

The normal case would be to annotate on a service layer level, but this really depends on your requirements. 通常的情况是在服务层级注释,但这实际上取决于您的要求。

Annotating on a service layer will result in longer transactions than annotating on DAO level. 在服务层上添加注释将导致比在DAO级别上注释更长的事务。 Depending on the transaction isolation level that can youse problems, as concurrent transactions wont see each other's changes in eg. 取决于可以解决问题的事务隔离级别,因为并发事务不会看到彼此的更改,例如。 REPEATABLE READ. 可重复阅读。

Annotating on the DAOs will keep the transactions as short as possible, with the drawback that the functionality your service layer is exposing wont be done in a single (rollbackable) transaction. 对DAO进行注释将使事务尽可能短,但缺点是服务层公开的功能不会在单个(可回滚)事务中完成。

It does not make sense to annotate both layers if the propagation mode is set to default. 如果传播模式设置为默认值,则注释两个图层没有意义。


#4楼

Transactional Annotations should be placed around all operations that are inseparable. Transactional Annotations应围绕所有不可分割的操作。

For example, your call is "change password". 例如,您的电话是“更改密码”。 That consists of two operations 这包括两个操作

  1. Change the password. 更改密码。
  2. Audit the change. 审核变更。
  3. Email the client that the password has changed. 通过电子邮件向客户端发送密码已更改。

So in the above, if the audit fails, then should the password change also fail? 那么在上面,如果审核失败,那么密码更改是否也会失败? If so, then the transaction should be around 1 and 2 (so at the service layer). 如果是这样,那么交易应该在1和2附近(所以在服务层)。 If the email fails (probably should have some kind of fail safe on this so it won't fail) then should it roll back the change password and the audit? 如果电子邮件失败(可能应该具有某种故障安全性,因此它不会失败)那么它应该回滚更改密码和审核吗?

These are the kind of questions you need to be asking when deciding where to put the @Transactional . 这些是您在决定将@Transactional放在何处时需要提出的问题。


#5楼

For Transaction in database level 对于数据库级别的事务

mostly I used @Transactional in DAO's just on method level, so configuration can be specifically for a method / using default (required) 大多数时候我只在方法级别使用DAO的@Transactional ,所以配置可以专门用于方法/使用默认值(必需)

  1. DAO's method that get data fetch (select .. ) - don't need @Transactional this can lead to some overhead because of transaction interceptor / and AOP proxy that need to be executed as well. DAO的获取数据的方法(选择..) - 不需要@Transactional这可能会导致一些开销,因为事务拦截器/和AOP代理也需要执行。

  2. DAO's methods that do insert / update will get @Transactional DAO的插入/更新方法将获得@Transactional

very good blog on 关于非常好的博客

For application level - 对于应用程序级别

I am using transactional for business logic I would like to be able rolback in case of unexpected error 我正在使用事务性的业务逻辑我希望能够在意外错误的情况下回滚

@Transactional(rollbackFor={MyApplicationException.class})public void myMethod(){    try {            //service logic here         } catch(Throwable e) {        log.error(e)        throw new MyApplicationException(..);    }}

#6楼

I place the @Transactional on the @Service layer and set rollbackFor any exception and readOnly to optimize the transaction further. 我将@Transactional放在@Service层并设置rollbackFor任何异常和readOnly以进一步优化事务。

By default @Transactional will only look for RuntimeException (Unchecked Exceptions), by setting rollback to Exception.class (Checked Exceptions) it will rollback for any exception. 默认情况下,@ @Transactional将仅查找RuntimeException (未经检查的异常),通过将回滚设置为Exception.class (Checked Exceptions),它将回滚任何异常。

@Transactional(readOnly = false, rollbackFor = Exception.class)

See . 请参阅已 。

转载地址:http://iuexb.baihongyu.com/

你可能感兴趣的文章
Valid Number
查看>>
Text Justification
查看>>
Simplify Path
查看>>
Add Two Numbers
查看>>
Longest Substring Without Repeating Characters
查看>>
Median of Two Sorted Arrays
查看>>
Search for a Range
查看>>
罗马数字与阿拉伯数字的相互转化
查看>>
3Sum
查看>>
Next Permutation
查看>>
sys文件系统
查看>>
Mysql常用命令大全
查看>>
Linux内核中C编程生僻用法(GNU C)
查看>>
辞职后五险一金怎么处理?
查看>>
几种开源的TCP/IP协议栈对比
查看>>
C语言之断言
查看>>
程序员技术练级攻略
查看>>
#define
查看>>
C语言之if...else PK switch...case
查看>>
关于SVN方面的问题
查看>>