一。前提:
1.前面有写过“数据库中的-脏读,幻读,不可重复读”,之所以要用到事务,也就是为了控制数据库的“锁”概念。地址是 http://blog.csdn.net/d8111/archive/2008/06/29/2595635.aspx
2.看spring文档,了解spring的5种事务级别,7种隔离级别。
二。总概
spinrg的编程风格非常一致,包括事务的配置及其他一些内容,均提供一下几种标准模式:
1.编程式注入事务:就是在代码中引入TransactionTemplate 模板显示调用模板方法。
如果不需要返回值,如下:
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
updateOperation1();
updateOperation2();
}
});
回调方法内的代码可以通过调用 TransactionStatus 对象的 setRollbackOnly() 方法来回滚事务。
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
updateOperation1();
updateOperation2();
} catch (SomeBusinessExeption ex) {
status.setRollbackOnly();
}
}
});
具体可以参考《The Spring Framework - Reference Documentation》,这里不难,但是也是最不推荐的一种方法。除非你需要精确控制一个方法中仅仅某几行加入事务,否则不推荐使用。
2。基于AOP的配置(通过spring的文档弄明白AOP是一个非常痛苦的事情,so many 概念,看起来十分复杂。而实际上,我们仅仅需要了解几行代码即可)。基于AOP的配置通常是用来处理批量事务。比如要讲所以get开头的方法加入readonly等等啦。。
<!---->
<beans xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns="http://www.springframework.org/schema/beans">
<bean class="x.y.service.DefaultFooService" id="fooService">
<!---->
<bean class="x.y.SimpleProfiler" id="profiler">
<!---->
<property name="order" value="1"></property>
</bean>
<!----><tx:annotation-driven order="200" transaction-manager="txManager"></tx:annotation-driven>
<!----><aop:config>
<!---->
<aop:aspect id="profilingAspect" ref="profiler">
<aop:pointcut id="serviceMethodWithReturnValue" expression="execution(!void x.y..*Service.*(..))"></aop:pointcut>
<aop:around method="profile" pointcut-ref="serviceMethodWithReturnValue"></aop:around>
</aop:aspect>
</aop:config>
<bean destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"></property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="txManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</bean></beans>
3。基于bean属性的xml注入配置方式。这种方式配置工作量比较大,建议使用方法4代替
1)将TransactionProxyFactoryBean声明成一个抽象bean,作为service层bean的基类,需要事务的bean集成此基类。
<!---->
<beans default-autowire="byName">
<bean class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true" id="transactionParent">
<property name="transactionManager"><ref bean="transactionManager"></ref></property>
</bean>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource">
<ref local="dataSource">
</ref></property>
</bean>
</beans>
2)业务bean继承事务基类,覆盖父类属性“target”配置事务属性
<bean parent="transactionParent" id="dosomethingBean">
<property name="target">
<bean class="com.alisoft.xxxx">
</bean>
<property name="transactionAttributes">
<props>
<prop key="commitScore"> //方法名
PROPAGATION_REQUIRED
</prop>
</props>
</property>
</property></bean>
4。基于annotation事务(需要添加cglib包)。这是一种针对单个方法加入事务最easy,我也最推荐的方式了。
1)在spring基于 Schema的配置xml中加入:
<tx:annotation-driven transaction-manager="transactionManager"/>
他的作用是: 开启annotations模式的事务行为
2)然后,在需要加入事务的类/方法上使用注释@Transactional。属性直接配置
/** * 这个方法被加入了事务控制
* @param departmentId
*/
@Transactional(isolation=Isolation.SERIALIZABLE)
public Integer getDepartmentArchiveCode(int departmentId) { }
分享到:
相关推荐
Spring Boot 2.7.x(贼新) Spring MVC MyBatis + MyBatis Plus 数据访问(开启分页) Spring Boot 调试工具和项目处理器 Spring AOP 切面编程 Spring Scheduler 定时任务 Spring 事务注解 业务特性: 业务代码生成...
python学习资源
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
基于Andorid的音乐播放器项目设计(国外开源)实现源码,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。
python学习资源
python学习资源
python学习一些项目和资源
【毕业设计】java-springboot+vue家具销售平台实现源码(完整前后端+mysql+说明文档+LunW).zip
HTML+CSS+JavaScarip开发的前端网页源代码
python学习资源
【毕业设计】java-springboot-vue健身房信息管理系统源码(完整前后端+mysql+说明文档+LunW).zip
成绩管理系统C/Go。大学生期末小作业,指针实现,C语言版本(ANSI C)和Go语言版本
1_基于大数据的智能菜品个性化推荐与点餐系统的设计与实现.docx
【毕业设计】java-springboot-vue交流互动平台实现源码(完整前后端+mysql+说明文档+LunW).zip
内容概要:本文主要探讨了在高并发情况下如何设计并优化火车票秒杀系统,确保系统的高性能与稳定性。通过对比分析三种库存管理模式(下单减库存、支付减库存、预扣库存),强调了预扣库存结合本地缓存及远程Redis统一库存的优势,同时介绍了如何利用Nginx的加权轮询策略、MQ消息队列异步处理等方式降低系统压力,保障交易完整性和数据一致性,防止超卖现象。 适用人群:具有一定互联网应用开发经验的研发人员和技术管理人员。 使用场景及目标:适用于电商、票务等行业需要处理大量瞬时并发请求的业务场景。其目标在于通过合理的架构规划,实现在高峰期保持平台的稳定运行,保证用户体验的同时最大化销售额。 其他说明:文中提及的技术细节如Epoll I/O多路复用模型以及分布式系统中的容错措施等内容,对于深入理解大规模并发系统的构建有着重要指导意义。
基于 OpenCV 和 PyTorch 的深度车牌识别
【毕业设计-java】springboot-vue教学资料管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
此数据集包含有关出租车行程的详细信息,包括乘客人数、行程距离、付款类型、车费金额和行程时长。它可用于各种数据分析和机器学习应用程序,例如票价预测和乘车模式分析。
把代码放到Word中,通过开发工具——Visual Basic——插入模块,粘贴在里在,把在硅基流动中申请的API放到VBA代码中。在Word中,选择一个问题,运行这个DeepSeekV3的宏就可以实现在线问答
【毕业设计】java-springboot+vue机动车号牌管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip