Spring 手动 提交事务
1、配置文件 applicationContext.xml:
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
2、在需要加事务的方法上加上
DataSourceTransactionManager transactionManager = (DataSourceTransactionManager) ctx
.getBean("txManager");
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别,开启新事务,这样会比较安全些。
TransactionStatus status = transactionManager.getTransaction(def); // 获得事务状态
try {
//逻辑代码,可以写上你的逻辑处理代码
transactionManager.commit(status);
} catch (Exception e) {
transactionManager.rollback(status);
}
3. ApplicationContext 不存在的情况下
@Autowired
private DataSourceTransactionManager txManager;
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);// 事物隔离级别,开启新事务
TransactionStatus status = txManager.getTransaction(def); // 获得事务状态
try{
//逻辑代码,可以写上你的逻辑处理代码
txManager.commit(status);
}catch(Exception e){
txManager.rollback(status);
}
3. Spring声明式事务配置
<!-- 事务配置 spring 3.0 -->
<beanid="transactionManager class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<aop:config>
<aop:pointcutexpression="execution(public * com.*.service.impl.*Impl.*(..))"id="pointcut"/>
<aop:advisoradvice-ref="txAdvice"pointcut-ref="pointcut"/>
</aop:config>
<tx:adviceid="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<tx:methodname="query*"propagation="REQUIRED"read-only="true"/>
<tx:methodname="find*"propagation="REQUIRED"read-only="true"/>
<tx:methodname="get*"propagation="REQUIRED"read-only="true"/>
<tx:methodname="save*"propagation="REQUIRED"/>
<tx:methodname="add*"propagation="REQUIRED"/>
<tx:methodname="create*"propagation="REQUIRED"/>
<tx:methodname="delete*"propagation="REQUIRED"/>
<tx:methodname="del*"propagation="REQUIRED"/>
<tx:methodname="remove*"propagation="REQUIRED"/>
<tx:methodname="modify*"propagation="REQUIRED"/>
<tx:methodname="update*"propagation="REQUIRED"/>
<tx:methodname="clear*"propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
在使用Spring声明式事务时,不需要手动的开启事务和关闭事务,但是对于一些场景则需要开发人员手动的提交事务,比如说一个操作中需要处理大量的数据库更改,可以将大量的数据库更改分批的提交,又比如一次事务中一类的操作的失败并不需要对其他类操作进行事务回滚,就可以将此类的事务先进行提交,这样就需要手动的获取Spring管理的Transaction来提交事务。
1、applicationContext.xml配置
1 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
2 <property name="dataSource" ref="dataSource" />
3 </bean>
4
5 <tx:advice id="txAdvice" transaction-manager="transactionManager">
6 <tx:attributes>
7 <tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
8 <tx:method name="find*" read-only="true" propagation="SUPPORTS" />
9 <tx:method name="get*" read-only="true" propagation="SUPPORTS" />
10 <tx:method name="select*" read-only="true" propagation="SUPPORTS" />
11 <tx:method name="list*" read-only="true" propagation="SUPPORTS" />
12 <tx:method name="load*" read-only="true" propagation="SUPPORTS" />
13 </tx:attributes>
14 </tx:advice>
15
16 <aop:config>
17 <aop:pointcut id="servicePointCut" expression="execution(* com.xxx.xxx.service..*(..))" />
18 <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointCut" />
19 </aop:config>
2、手动提交事务
1 @Resource(name="transactionManager")
2 private DataSourceTransactionManager transactionManager;
3
4 DefaultTransactionDefinition transDefinition = new DefaultTransactionDefinition();
5 //开启新事物
6 transDefinition.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRES_NEW);
7 TransactionStatus transStatus = transactionManager.getTransaction(transDefinition);
8 try {
9 //TODO
10 transactionManager.commit(transStatus);
11 } catch (Exception e) {
12 transactionManager.rollback(transStatus);
13 }
Spring 手动 提交事务的更多相关文章
- Spring 手动提交事务
在使用Spring声明式事务时,不需要手动的开启事务和关闭事务,但是对于一些场景则需要开发人员手动的提交事务,比如说一个操作中需要处理大量的数据库更改,可以将大量的数据库更改分批的提交,又比如一次事务 ...
- Spring手动提交事务
// name的值根据spring配置文件的事物管理器的id而定 @Resource(name="transactionManager") private DataSourceTr ...
- Java中手动提交事务
项目中遇到一个问题,就是在程序的执行过程中需要不断地更新某个信息,但是在springmvc中好像是默认不可以的,那么就需要手动提交 // 从spring容器对象中获取DataSourceTransac ...
- mysql中,手动提交事务
1: 在mysql中,手动提交事务的案例:CREATE PROCEDURE tfer_funds (from_account int, to_account int, tfer_amoun ...
- (后端)Spring手动回滚事务
百度上查资料获得的 throw new RuntimeException(); 或者 TransactionAspectSupport.currentTransactionStatus().setR ...
- spring手动配置
本文总结自:https://www.cnblogs.com/V1haoge/p/7183408.html SpringBoot中免除了大部分配置,但是对于一些特定的情况,还是需要我们进行手动配置的. ...
- 使用spring手动获取Bean的时候,不能强转回它自己。
这个问题好像有点长,描述一下: 就是通过类名的方式获取Bean后,得到一个Object对象,但是这个Object不能再强转回Bean了.抛出的异常时类型转换异常. java.lang.ClassCa ...
- springboot 开启事务以及手动提交事务
添加依赖,sprongboot 会默认开启事务管理 org.springframework.boot spring-boot-starter-jdbc 在需要的服务类里添加注解 @Autowired ...
- 【Oracle】使用bbed手动提交事务
有时候数据库挂掉,起库会出现ORA-00704错误,而导致ORA-00704错误的根本原因是訪问OBJ$的时候.ORACLE须要回滚段中的数据,而訪问回滚段的时候须要的undo数据已经被覆盖,此时我们 ...
随机推荐
- Vue项目,运行出现warning(Emitted value instead of an instance of Error)
组件:<XXXX v-for="item in items" /> warning:(Emitted value instead of an instance of E ...
- CentOS7自定义安装git
1. 介绍 使用Coding管理项目,上面要求使用的git版本为1.8.0以上,而很多yum源上自动安装的git版本为1.7,所以需要掌握手动编译安装git方法. 2. 安装git依赖包yum ins ...
- mybatis foreach 循环 list(map)
直接上代码: 整体需求就是: 1.分页对象里面有map map里面又有数组对象 2.分页对象里面有list list里面有map map里面有数组对象. public class Page { pri ...
- Python :数据结构
LearnPython :数据结构 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .labe ...
- 24.API爬天气预报数据
1.免费注册API 地址: https://console.heweather.com/ 必须要用IE浏览器打开,注册邮箱激活,打开控制台,如图: 认证key是访问api的钥匙 2.阅读api说明开发 ...
- 在IAR调用Notepad++
之前写过在keil调用Notepad++,这次讲一下怎么在IAR调用Notepad++. 好了上步骤: 打开IAR软件,选择‘Tools’-->'Configure Tools' 2.如下图,在 ...
- NRF51822之RNG
在裸机下官方已经提供另一个RNG的例子(RF51_SDK_10.0.0_dc26b5e\examples\peripheral\rng) 好了现在我将给出在蓝牙模式下如何使用例子 #include & ...
- Deque 双端队列 Stack 堆栈
1. peek 获取栈顶元素 pop 删除栈顶元素
- mysql 日期时间运算函数
时期时间函数 DAYOFWEEK(date) 返回日期date是星期几(1=星期天,2=星期一,……7=星期六,ODBC标准)mysql> select DAYOFWEEK('1998-02-0 ...
- python 阿狸的进阶之路(6)
常用模块 json # 序列化 #将内存的数据存到硬盘中,中间的格式,可以被多种语言识别,跨平台交互数据 #json 可以将字典之类的数据类型存到字典中 import json dic = {&quo ...