Spring @Transactional Annotation Best Practice
We are currently discussing the Best Practice for placing the @Transactional annotations in our code.
Do you place the @Transactional in the DAO classes and/or their methods or is it better to annotate the Service classed which are calling using the DAO objects? Or does it make sense to annotate both "layers"?
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.
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. 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.
It does not make sense to annotate both layers if the propagation mode is set to default.
分享到:
相关推荐
主要介绍了Spring @Transactional工作原理详解,具有一定借鉴价值,需要的朋友可以参考下。
我們使用Spring Framework裡,在Service 的 Method 給予@Transactional 系統就會去管理transaction,我們是在哪裡做了甚麼設定嗎? 為什麼下的這個Annotation成功會commit失敗會rollback呢? Transaction 管理做了...
主要介绍了spring @Transactional 无效的解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
spring事务管理注解jar,spring-tx-3.2.4.RELEASE.jar,导入项目即可
@Transactional实现原理.txt
Spring3事务管理——使用@Transactional 注解.rar
在本篇文章里小编给大家整理了关于Spring声明式事务@Transactional详解内容,需要的朋友们可以参考下。
主要介绍了Spring @Transactional注解失效解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了深入学习Spring Boot排查 @Transactional 引起的 NullPointerException问题,需要的朋友可以参考下
Spring中@Transactional事务回滚(含实例详细讲解,附源码)Java开发Java经验技巧共8页.pdf.zip
Spring中的@Transactional事物回滚实例源码
本代码使用H2内存数据库演示spring事务使用,包括编程式事务,声明式事务@Transactional使用,自定义事务事务注解实现自定义事务管理器
今天我们来探讨一个有意思的spring源码问题,也是一个学生告诉了我现象我从源码里面找到了这个有意思的问题。 首先我们看service层的代码案例,如下: @Service("transationServiceImpl") public class ...
今天小编就为大家分享一篇关于springboot中事务管理@Transactional的注意事项与使用场景,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
本篇文章主要介绍了浅谈Spring中@Transactional事务回滚及示例(附源码),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
声明式事务管理也有两种常用的方式,一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解。显然基于注解的方式更简单易用,
Spring源码学习十二:@Transactional是如何工作的1
主要介绍了spring的@Transactional注解用法解读,具有一定借鉴价值,需要的朋友可以参考下
关于spring中@Transactional注解传播属性原理的实验 具体方法: 主体形式:a方法调用b方法 a插入数据“one” b插入数据“two” a、b方法都可以有不同的传播级别或者不加事务注解(none): required(rd), required_...