@Transactional
Defines how transactions relate to each other. Common options
-
Required
: Code will always run in a transaction. Create a new transaction or reuse one if availble. -
Requires_new
: Code will always run in a new transaction. Suspend current transaction if one exist.
Defines the data contract between transactions.
-
Read Uncommitted
: Allows dirty reads -
Read Committed
: Does not allow dirty reads -
Repeatable Read
: If a row is read twice in the same transaciton, result will always be the same -
Serializable
: Performs all transactions in a sequence
The different levels have different performance characteristics in a multi threaded application. I think if you understand the dirty reads
concept you will be able to select a good option.
Example when a dirty read can occur
thread 1 thread 2||
write(x)|||| read(x)||
rollback |
v v
value (x) is now dirty (incorrect)
So a sane default (if such can be claimed) could be Read Comitted
, which only lets you read values which have already been comitted by other running transactions, in combination with an isolation level of Required
. Then you can work from there if you application has other needs.
A practical example where a new transaction will always be created when entering the provideService
routine and completed when leaving.
publicclassFooService{privateRepository repo1;privateRepository repo2;@Transactional(propagation=Propagation.REQUIRES_NEW)publicvoid provideService(){
repo1.retrieveFoo();
repo2.retrieveFoo();}}
Had we used Required
instead the transaction will remain open if the transaction was already open when entering the routine. Note also that the result of a rollback
could be different as several executions could take part in the same transaction.
We can easily verify the behaviour with a test and see how results differ with propagation levels
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations="classpath:/fooService.xml")publicclassFooServiceTests{private@AutowiredTransactionManager;private@AutowiredFooService fooService;@Testpublicvoid testProvideService(){TransactionStatus status = transactionManager.getTransaction(newDefaultTransactionDefinition());
fooService.provideService();
transactionManager.rollback(status);// assert repository values are unchanged ... }
相关推荐
声明式事务管理也有两种常用的方式,一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解。显然基于注解的方式更简单易用,
1、aopalliance.jar 这个包是AOP联盟的API包,里面包含了针对面向切面的接口。(通常Spring等其它具备动态织入功能的框架依赖此包) 2、aspectjrt.jar 处理事务和AOP所需的包 3、aspectjweaver.jar 处理事务和AOP所需...
Spring通过@Transactional注解以及底层的AOP和代理机制实现了声明式事务。这个机制允许开发者通过简单的注解就能控制事务的边界和规则,极大地简化了编程模型。在Spring中,事务管理是通过一系列的拦截器和事务管理...
在上添加@Transactional注解并不会完成事务切面的织入,还需要在配置文件中加一行配置,通知spring容器对标了@Transactional的bean进行处理,织入事务。 在applicationContext-tx.xml文件上加上tx:annotation-driven...
6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1....
spring注解 XML配置AOP的一个实例
springboot-jdbc-aop-transactional-demo
声明式事务管理也有两种常用的方式,一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解。显然基于注解的方式更简单易用,更清爽。spring注解事务实现demo
一头扎进SpringBoot ...第一节:SpringBoot之事物管理@Transactional 第六章:SpringBoot之表单验证 第一节:SpringBoot之表单验证@Valid 第七章:SpringBoot之切面AOP 第一节:SpringBoot之切面AOP
6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明一个切面 6.2.3. 声明一个...
6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 ...
spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解),源码包,包含数据库表文件
- chapter1:[基本项目构建(可作为工程脚手架),引入...由于Spring Cloud偏宏观架构,Spring Boot偏微观细节,内容上越来越多,为了两部分内容不互相干扰,所以迁移Spring Cloud内容到:[SpringCloud-Learning项目]...
6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...
网上的文章要么版本太老有些过时,要么用的不是mybatis-plus而是mybaits,要么步骤繁琐、需要自己手动编写aop切面代码,要么在同一service层方法中只能使用@Transactional实现单个数据源的事务管理控制,总是觉得...
234 <br>10.5.2.5 用Spring 2.0 的@Transactional标注解决事务提交问题(最佳方案) 238 <br>10.5.2.6 使用 HibernateTemplate 实现分页查询... 241 <br>10.6 小结... 242 <br>10.7 参考资料... 242...
6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明一个切面 6.2.3. 声明一个切入点...
控制器将请求路由到Service类,Spring AOP( UserServiceAspect类)拦截服务调用并设置Hibernate租户过滤器。 必须使用@Transactional注释所有服务方法, UserServiceAspect才能正常工作。 上面的方法仅适用于...
10.6.1 哪些方法不能实施Spring AOP事务 10.6.2 事务增强遗漏实例 10.7 数据连接泄漏 10.7.1 底层连接资源的访问问题 10.7.2 Spring JDBC数据连接泄漏 10.7.3 通过DataSourceUtils获取数据连接 10.7.4 通过...
Passing the exam demonstrates your understanding of Spring and validates your familiarity with: container-basics, aspect oriented programming (AOP), data access and transactions, Spring Security, ...