• 使用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

  • 相关阅读:
    POJ2762 Going from u to v or from v to u?
    POJ3422或洛谷2045 Kaka's Matrix Travels
    LaTeX数学公式大全
    POJ1966 Cable TV Network
    转:Android中的Handler的机制与用法详解
    Httpservlet cannot be resolved to a type的原因与解决方法
    Textchangedlistener的用法
    contextMenu的使用
    转:android studio 一直卡在Gradle:Build Running的解决办法
    Sqlite操作的一些关键类的官方说明与Intent的startactivityforresult方法
  • 原文地址:https://www.cnblogs.com/yangzhilong/p/3311697.html
Copyright © 2020-2023  润新知