1、编程式事务管理

spring的配置文件

    <!-- 事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource"><ref bean="dataSource" /></property></bean><!-- 事务回滚 --><bean id="defaultTransactionDefinition"class="org.springframework.transaction.support.DefaultTransactionDefinition"></bean>

java代码:

@Resource
private DataSourceTransactionManager transactionManager;
@Resource
private DefaultTransactionDefinition defaultTransactionDefinition;public void delete(String id) throws Exception{TransactionStatus status = transactionManager.getTransaction(defaultTransactionDefinition);try{this.dao.delete(id);transactionManager.commit(status);//没有发生异常提交事务} catch(Exception e){transactionManager.rollback(status);//发生了异常,回滚事务
      log.warn(e.getMessage());throw e;}
}

2、基于注解的事务(注解只能作用于实现了接口的service或者dao),作用于类或者方法

spring的配置文件中加入:

<!-- 事务管理器 -->
<bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource"><ref bean="dataSource" /></property>
</bean><!-- 开启注解的事务配置功能 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<tx:annotation-driven/> 的属性

Attribute Default Description
transaction-manager transactionManager

Name of transaction manager to use. Only required if the name of the transaction manager is not transactionManager, as in the example above.

mode proxy

The default mode "proxy" processes annotated beans to be proxied using Spring's AOP framework (following proxy semantics, as discussed above, applying to method calls coming in through the proxy only). The alternative mode "aspectj" instead weaves the affected classes with Spring's AspectJ transaction aspect, modifying the target class byte code to apply to any kind of method call. AspectJ weaving requires spring-aspects.jar in the classpath as well as load-time weaving (or compile-time weaving) enabled. (See Section 7.8.4.5, “Spring configuration” for details on how to set up load-time weaving.)

proxy-target-class false

Applies to proxy mode only. Controls what type of transactional proxies are created for classes annotated with the @Transactionalannotation. If the proxy-target-class attribute is set to true, then class-based proxies are created. If proxy-target-class is false or if the attribute is omitted, then standard JDK interface-based proxies are created. (See Section 7.6, “Proxying mechanisms” for a detailed examination of the different proxy types.)

order Ordered.LOWEST_PRECEDENCE

Defines the order of the transaction advice that is applied to beans annotated with @Transactional. (For more information about the rules related to ordering of AOP advice, see Section 7.2.4.7, “Advice ordering”.) No specified ordering means that the AOP subsystem determines the order of the advice.

代码中加入

@Transactional(readOnly=false,propagation=Propagation.REQUIRED)

@Transactional 注解的属性

属性 类型 描述
传播性(propagation) 枚举型:Propagation 可选的传播性设置
隔离性(isolation) 枚举型:Isolation 可选的隔离性级别(默认值:ISOLATION_DEFAULT
只读性(readOnly) 布尔型 读写型事务 vs. 只读型事务
超时(timeout) int型(以秒为单位) 事务超时
回滚异常类(rollbackFor) 一组 Class 类的实例,必须是Throwable 的子类 一组异常类,遇到时 必须 进行回滚。默认情况下checked exceptions不进行回滚,仅unchecked exceptions(即RuntimeException的子类)才进行事务回滚。
回滚异常类名(rollbackForClassname) 一组 Class 类的名字,必须是Throwable的子类 一组异常类名,遇到时 必须 进行回滚
不回滚异常类(noRollbackFor) 一组 Class 类的实例,必须是Throwable 的子类 一组异常类,遇到时 必须不 回滚。
不回滚异常类名(noRollbackForClassname) 一组 Class 类的名字,必须是Throwable 的子类 一组异常类,遇到时 必须不 回滚

3、声明性事务

见我的另外一篇博文:http://www.cnblogs.com/yangzhilong/archive/2013/02/04/2891819.html

使用spring的事务的三种方法相关推荐

  1. Spring装配bean的三种方法:自动化装配,java代码装配,XML装配及它们的混合使用

    一.自动化装配 首先,把可能被装配的类声明为组件类,告知spring要为这个类创建bean如: import org.springframework.stereotype.Component;@ Co ...

  2. Spring通过工厂创建对象的三种方法详解(工厂设计模式)

    1.场景描述 在创建对象的过程中,常常创建出的对象并不能直接使用,它可能需要若干步复杂的步骤,处理完成后才能正常使用.比如有一个网络连接的类NetConn,类中有加载配置文件的方法load(),测试网 ...

  3. Spring框架–应用程序上下文–到达应用程序上下文的三种方法

    本文向您展示了三种不同的方式来获取代码中的Spring Framework Application Context. 摘要 (这是我在2010年撰写的旧文章的转贴). 在Google中搜索" ...

  4. Struts2中action接收参数的三种方法及ModelDriven跟Preparable接口结合JAVA反射机制的灵活用法...

    Struts2中action接收参数的三种方法及ModelDriven跟Preparable接口结合JAVA反射机制的灵活用法 www.MyException.Cn   发布于:2012-09-15 ...

  5. mysql改存储引擎教程_MySQL中修改数据表存储引擎的三种方法

    第一种方法:ALTER TABLE 将表从一个引擎修改为另一个引擎最简单的办法是使用ALTER TABLE语句,转换表的存储引擎会导致失去原引擎相关的所有特性. 例:将mytable的引擎修改为Inn ...

  6. spring 获取 WebApplicationContext的几种方法

    spring 获取 WebApplicationContext的几种方法 使用ContextLoader WebApplicationContext webApplicationContext = C ...

  7. Iterator to list的三种方法

    文章目录 简介 使用while 使用ForEachRemaining 使用stream 总结 Iterator to list的三种方法 简介 集合的变量少不了使用Iterator,从集合Iterat ...

  8. Spring 获取 request 的几种方法及其线程安全性分析

    转载自  Spring 获取 request 的几种方法及其线程安全性分析 本文将介绍在Spring MVC开发的Web系统中,获取request对象的几种方法,并讨论其线程安全性. 一.概述 在使用 ...

  9. 安装软件包的三种方法 ,rpm包介绍 , rpm工具用法, yum工具用法, yum搭建本地仓库...

    linux安装和卸载软件 安装三种方法 rpm工具 yum工具 源码包 rpm工具使用 首先看一下什么事rpm包,可以挂载光驱看看里面有很多 [root@localhost ~]# mount /de ...

  10. mysql添加临时索引_mysql 中添加索引的三种方法

    在mysql中有多种索引,有普通索引,全文索引,唯一索引,多列索引,小伙伴们可以通过不同的应用场景来进行索引的新建,在此列出三种新建索引的方法 mysql 中添加索引的三种方法 1.1 新建表中添加索 ...

最新文章

  1. RuntimeError: output with shape [1, 28, 28] doesnt match the broadcast shape [3, 28, 28]
  2. keepalive+nginx实现负载均衡高可用_超详细的LVS+keepalived+nginx实现高性能高可用负载均衡集群教程...
  3. 修改参数failed_login_attempts=unlimited
  4. 十二:NodeManager
  5. 质数环问题c语言,素数环问题
  6. 批处理:修改COM端口号
  7. LeetCode 642. 设计搜索自动补全系统(Trie树)
  8. 算法笔记之——快速幂
  9. 微博正式登陆港交所挂牌上市:开盘破发 较发行价跌6.1%
  10. HDU-1874畅通工程续( 最短路)
  11. sublime text 3 3143
  12. IEWebBrowser
  13. android init.rc 添加指令三部曲
  14. 4月3日 今天谈谈支持向量机SVM 与超平面
  15. 云计算数据中心网络性能测试
  16. 这些年java全栈开发涉及到工具
  17. 天才黑客 Flanker 疑因拒绝做黑客攻击业务,被拼多多强行辞退,错失上亿股票...
  18. excel文字显示图标集_创建自己的Excel图标集
  19. React中的ref属性的使用
  20. 【天池】金融风控-贷款违约预测(五)—— 模型融合

热门文章

  1. ora-01950:对表空间XXX无权限
  2. hdu1536 S-Nim(博弈)
  3. PHP 开源搜索引擎Yioop! 0.80 发布
  4. 测试Live Writer Beta2功能
  5. maven解析xml+测试test+注解
  6. SpringBoot源码篇:Spring5内置tomcat实现code-based的web.xml实现
  7. Linux下安装zookeeper集群,以及在window用dubbo和代码测试
  8. Bag-of-words模型、TF-IDF模型
  9. CSS实现输入框的高亮效果-------Day50
  10. CSS3背景渐变。。。