描述

事务还是一个比较好的东东,有了这个,我们在做流程性的东西的时候,就会很好,很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);
}

这是在有 ApplicationContext 的情况下、

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>

SpringMVC 手动控制事务提交的更多相关文章

  1. SpringMVC 手动控制事务提交 【转】

    1.配置文件 applicationContext.xml: <bean id="txManager" class="org.springframework.jdb ...

  2. springboot之手动控制事务

    一.事务的重要性,相信在实际开发过程中,都有很深的了解了.但是存在一个问题我们经常在开发的时候一般情况下都是用的注解的方式来进行事务的控制,说白了基于spring的7种事务控制方式来进行事务的之间的协 ...

  3. 关于SAP的事务提交和回滚(LUW)

    1 Sap的更新的类型 在sap中,可以使用CALL FUNCTION ... IN UPDATE TASK将多个数据更新绑定到一个database LUW中.程序使用COMMIT WORK提交修改请 ...

  4. mysql源码解读之事务提交过程(二)

    上一篇文章我介绍了在关闭binlog的情况下,事务提交的大概流程.之所以关闭binlog,是因为开启binlog后事务提交流程会变成两阶段提交,这里的两阶段提交并不涉及分布式事务,当然mysql把它称 ...

  5. mysql源码解读之事务提交过程(一)

    mysql是一种关系型数据库,关系型数据库一个重要的特性就是支持事务,这是区别于no-sql产品的一个核心特性.当然了,no-sql产品支持键值查询,不能支持sql语句,这也是一个区别.今天主要讨论下 ...

  6. mysql 事务提交过程

     打开binlog选项后,执行事务提交命令时,就会进入两阶段提交模式.两阶段提交分为prepare阶段和commit两个阶段.流程如下 :这里面涉及到两个重要的参数:innodb_flush_log_ ...

  7. MySQL(21):事务管理之 事务提交

    1. 现实生活中,许多操作都是需要用户确认的,例如用户删除一个文档,删除时候会弹出一个提示对话框,包含"确认"和"取消".同样的道理,在数据库中有些命令在使用的 ...

  8. PHP 中mysql如何实现事务提交?

    事务就是指对数据库的多次修改,要么全部成功,要么全部失败,不能出现部分修改成功,部分修改失败的情况. PHP下操作mysql数据库要实现事务提交,需注意以下方面: 1, 数据库表存储引擎类型设置为in ...

  9. springMVC之事务配置(问题来源:为什么数据保存不了)

    参考文章:http://www.cnblogs.com/leiOOlei/p/3725911.html 自己的亲身体会,来源问题this.sessionFactory.getCurrentSessio ...

随机推荐

  1. gitlab的使用方法

    Git global setup: git全局建立 git config --global user.name "Your Name" git config --global us ...

  2. Android:The connection to adb is down, and a severe error has occured.解决方法一

    在自己机上打安桌虚拟机,竟然提示“The connection to adb is down, and a severe error has occured.please ensure ......” ...

  3. HDU1051 贪心

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. 使用redis的五个注意事项

    http://blog.nosqlfan.com/html/3705.html 下面内容来源于Quora上的一个提问,问题是使用Redis需要避免的五个问题.而回答中超出了五个问题的范畴,描述了五个使 ...

  5. linux卸载mysql,apache,php

    卸载Mysql 1.查找以前是否装有mysql 命令:rpm -qa|grep -i mysql 可以看到mysql的包: mysql-3.23.58-9php-mysql-4.3.4-11mod_a ...

  6. NYOJ-组合数

    #include <stdio.h> #include <malloc.h> int main() { ; ]; scanf("%d%d", &n, ...

  7. C++11---nullptr

    1.nullprt与NULL 代码: void f(int i) {    cout << "f(int)" << endl;} void f(char* ...

  8. 20145218&20145240 《信息安全系统设计基础》实验一 开发环境的熟悉

    20145218&20145240 <信息安全系统设计基础>实验一 开发环境的熟悉 课程:信息安全系统设计基础 班级:1452 姓名:(按贡献大小排名)刘士嘉 张晓涵 学号:(按贡 ...

  9. 一个支持FMX.Win框架的托盘控件

    不多说了 直接上代码........有任何问题请给我邮件.... // **************************************************************** ...

  10. resx文件在X64位编译,提示“未能加载文件或程序集”的问题?

    原文:resx文件在X64位编译,提示"未能加载文件或程序集"的问题? resx文件在X64位编译,提示"未能加载文件或程序集"的问题? 解答: 错误现象如下 ...