zoukankan      html  css  js  c++  java
  • 使用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-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

  • 相关阅读:
    软件系统的稳定性
    项目从.net 2.0 升级到。.net 4.0项目以后发现网站运行十分缓慢
    学习英语小助手(阅读粘贴的英文,使用MVVM)
    如何在IIS6,7中部署ASP.NET网站
    基于 IOCP 的通用异步 Windows Socket TCP 高性能服务端组件的设计与实现
    面向对象软件设计原则—— 软件实体的设计原则
    Django实战
    聊聊豆瓣阅读kindle版
    多线程的基本概念
    nopCommerce的源代码结构和架构
  • 原文地址:https://www.cnblogs.com/yangzhilong/p/3311697.html
Copyright © 2011-2022 走看看