使用spring的事务的三种方法

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/> 的属性

AttributeDefaultDescription
transaction-managertransactionManager

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

modeproxy

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-classfalse

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.)

orderOrdered.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值