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 实现事务的三种方式

    事务:保证数据的运行不会说A给B钱,A钱给了B却没收到. 实现事务的三种方式(重要代码): 1.aspectJ AOP实现事务: <bean id="dataSourceTransac ...

  2. spring集成JPA的三种方法配置

    JPA是Java EE5规范之一,是一个orm规范,由厂商来实现该规范.目前有hibernate,OpenJPA,TopLink和EclipseJPA等实现 spring提供三种方法集成JPA:1.L ...

  3. Spring使用jdbcJdbcTemplate和三种方法配置数据源

    三种方法配置数据源 1.需要引入jar包:spring-jdbc-4.3.2.RELEASE.jar <!-- spring内置,springJdbc,配置数据源 --> <bean ...

  4. spring注入bean的三种方法

    在Spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...

  5. Spring -- 配置bean的三种方法

    配置通过静态工厂方法创建的bean public class StaticBookFactory { //静态工厂方法: public static Book getBook(String bookN ...

  6. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring实例化Bean的三种方法

    在面向对象的程序中,要想调用某个类的成员方法,就需要先实例化该类的对象.在 Spring 中,实例化 Bean 有三种方式,分别是构造器实例化.静态工厂方式实例化和实例工厂方式实例化. 构造器实例化 ...

  7. Spring实例化bean的三种方法

    1.用构造器来实例化 <bean id="hello2" class="com.hsit.hello.impl.ENhello" /> 2.使用静态 ...

  8. Spring配置数据源的三种方法

    前言:今天接触新项目发现用的是JNDI配置数据源,用度娘倒腾了一会也没弄好,只好用平常用的方法,结果发现BasicDataSource和DriverManagerDataSource也是不同的,所以记 ...

  9. Spring实例化Bean三种方法:构造器、静态工厂、实例工厂

    Spring中Bean相当于java中的类,可以通过xml文件对bean进行配置和管理. 一.Bean的实例化: 构造器实例化.静态工厂实例化.实例工厂方式实例化. 目录: 构造器实例化: xml配置 ...

随机推荐

  1. 算法:插入排序(Insertion Sort)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. unity 质量设置 Quality Settings

    Unity allows you to set the level of graphical quality it will attempt to render. Generally speaking ...

  3. Unity3d-Particle System 5.x系统的学习(四)

    Unity3d-Particle System 5.x系统的学习(四) 今天,我们来聊聊unity5.x的粒子系统和unity4.x粒子系统的区别. 我大致看了下,区别还是蛮多的,但是总体的粒子制作思 ...

  4. 学习笔记:状态压缩DP

    我们知道,用DP解决一个问题的时候很重要的一环就是状态的表示,一般来说,一个数组即可保存状态.但是有这样的一些题 目,它们具有DP问题的特性,但是状态中所包含的信息过多,如果要用数组来保存状态的话需要 ...

  5. 3D屏保: 彩色盘子

    一个彩色盘子的屏保 记得小时候在电视上看过一个科普节目,由多个颜色组成的盘子,如果快速旋转起来,会看上去是白色的.于是我就写了这个屏保程序,但发现在计算机上模拟并不是这样的. "RollPl ...

  6. data-stream-as-disjoint-intervals

    https://leetcode.com/problems/data-stream-as-disjoint-intervals/ /** * Definition for an interval. * ...

  7. go语言基础之结构体普通变量初始化

    1.结构体 1.1.结构体类型 有时我们需要将不同类型的数据组合成一个有机的整体,如:一个学生有学号/姓名/性别/年龄/地址等属性.显然单独定义以上变量比较繁琐,数据不便于管理. 结构体是一种聚合的数 ...

  8. go语言基础之map介绍和使用

    1.map介绍 Go语言中的map(映射.字典)是一种内置的数据结构,它是一个无序的key—value对的集合,比如以身份证号作为唯一键来标识一个人的信息. 2.map示例 map格式为: map[k ...

  9. js cookie实例

    什么是cookie:           △ 用来保存用户信息:用户名.密码... ...           △ 同一网站共享一套cookie,大小有限,保存时间           △ 使用doc ...

  10. android 百度地图demo 随感

    最近项目组的老大要我对百度的android的sdk进行一段的预研,由于技术太菜,出了不少的错误,因此有一点感悟了. 嗨,这个错误浪费了我一天的时间的时候,我按照百度的技术文档一步步的来做,每部基本上都 ...