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. iOS 5解决Could not instantiate class named NSLayoutConstraint问题

    如果使用Xcode 4.5来新建项目,默认是支持AutoLayout的,但是AutoLayout是iOS 6的新特性,如果在iOS 5的simulator上运行程序,会出现Could not inst ...

  2. 几款开源的ETL工具及ELT初探

    ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract).转换(transform).加载(load)至目的端的过程.ETL 是构建数据仓 ...

  3. 栅栏加解密python实现(支持密钥加密)

    栅栏加解密是对较短字符串的一种处理方式.给定行数Row,依据字符串长度计算出列数Column,构成一个方阵. 加密过程:就是按列依次从上到下对明文进行排列,然后依照密钥对各行进行打乱.最后以行顺序从左 ...

  4. RestClient POST

    SSL: ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyError ...

  5. LODOP打印控件之LODOP.NewPageA()方法

    通过lodop的强制分页方法,当我们一次性需要打印多张单页面的时候,可以通过该方法实现一次性打出,而不是重复调用打印方法,容易出问题 LODOP.NewPageA(); 说明:强制分页,注意是.New ...

  6. Ubuntu双系统安装

    原文链接: http://www.jianshu.com/p/2eebd6ad284d   作者 Volcanoo 2016.01.31 00:07 字数 1737 阅读 141859评论 161喜欢 ...

  7. iOS:UIButton按钮的详解

    UIButton的详细介绍: 一.按钮具有的属性: @property(nonatomic,readonly) UIButtonType buttonType;  //按钮形状类型 @property ...

  8. Java 动态向 JTable 中添加数据

    import java.awt.Toolkit; import javax.swing.SwingUtilities; import javax.swing.UIManager; import jav ...

  9. 简单MapReduce思维导图

  10. [leetcode]Populating Next Right Pointers in Each Node @ Python

    原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/ 题意: 1 / \ 2 3 / \ ...