• 首页

  • 笑一笑

  • 相册

  • 文学

  • 日志

  • 关于页面
u f a n y
u f a n y

英杰

获取中...

10
02

SpringMVC 手动控制事务提交

发表于 2020-10-02 • 被 1,634 人看爆

好记忆不如烂笔头 ,还是记下点东西比较好。


描述


       事务还是一个比较好的东东,有了这个,我们在做流程性的东西的时候,就会很好,很nice。

现在看看 SpringMVC 如何实现的,详细请看代码:

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);
}

<span class="pun" style="line-height:28px; color:rgb(102,102,0)">这是在有 ApplicationContext 的情况下、</span>

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的配置基本都ok了、虽然已经配置了好多次了,但是这次还是写写吧,自己都快忘记了。
贴上去的都是干货。欢迎指正和批评!



分享到:
Spring 学习笔记①:IoC容器、Bean与注入
linux 安装 expect tcl
  • 文章目录
  • 站点概览
英杰

帅哥英杰

QQ Email RSS
看爆 Top5
  • 使用 MHA 实现 MySQL 主从复制高可用 3,214次看爆
  • nginx反向代理配置去除前缀 2,960次看爆
  • 国产微服务网关-Soul(真香) 2,884次看爆
  • Kubernetes - Xshell连接虚拟机 & 搭建Kubernetes基础集群 2,663次看爆
  • 不把一切看得太重,生命才会更加洒脱 2,628次看爆

Copyright © 2025 英杰 · 冀ICP备19001756号-1

Proudly published with Halo · Theme by fyang · 站点地图