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. C#和C++中char类型的区别

    对于char,这个字符类型.我们一般都认为就是一个字节.今天在仔细比较发现,C#的char和C++的char是有区别的. 1.首先来看C#中char占多大空间 using System;using S ...

  2. Bootstrap 3之美05-排版、Button、Icon、Nav和NavBar、List、Table、Form

    本篇主要包括: ■  排版■  Button■  Icon■  Nav和NavBar■  List■  Table■  Form 排版 ● 斜体:<em>● 加粗体:<strong& ...

  3. java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener

    一:如果出现下面的错误信息,如果你的项目是Maven结构的,那么一般都是你的项目的Maven Dependencies没有添加到项目的编译路径下: 信息: The APR based Apache T ...

  4. UITableView分页

    UITableView分页上拉加载简单,ARC环境,源码如下,以作备份: 原理是,点击最后一个cell,触发一个事件来处理数据,然后reloadData RootViewController.m + ...

  5. 通过http请求传递xml流和接收xml流的代码示例

    通过http请求传递xml流和接收xml流的代码示例 //1.在servlet中post一个xml流:import java.io.OutputStreamWriter;import org.jdom ...

  6. Oracle数据库中违反唯一约束的处理

    根据NULL的定义,NULL表示的是未知,因此两个NULL比较的结果既不相等,也不不等,结果仍然是未知.根据这个定义,多个NULL值的存在应该不违反唯一约束. 实际上Oracle也是如此实现的: SQ ...

  7. hue解决timed out(code THRIFTSOCKET):None

    报错栈: Traceback (most recent call last): File , in decorator return func(*args, **kwargs) File , in e ...

  8. [MAC OS] XCode中的Debug View Hierarchy功能

    reference to : http://blog.csdn.net/liujinlongxa/article/details/46490949 前言 做iOS开发经常会遇见这种情况,产品汪拿着你做 ...

  9. @SpringContext通过实现ApplicationContextAware接口动态获取bean

    场景: 在代码中需要动态获取spring管理的bean 目前遇到的主要有两种场景:1.在工具类中需要调用某一个Service完成某一个功能,如DictUtils2.在实现了Runnable接口的任务类 ...

  10. PHPnow For ASP&&ASP.NET&&MongoDB&&MySQL支持VC6.0编译器&&MySQL升级

    可能和大家熟悉的是LAMP,Linux+Apache+Mysql+PHP,在Windows上,可能大家比较熟悉的是WAMP,Windows+Apache+Mysql+PHP,这是一个集成环境,说到集成 ...