ssm事务管理


注解方式:

@Transactional

 

spring配置中: 

xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd"



<!--事务管理器-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--启用事务的注解支持-->
    <stx:annotation-driven transaction-manager="txManager"/>

 

  用法:

当执行过程中第二次循环会产生一个异常,所以第一次执行的insert操作在发生异常时并不会插入到数据库中

   @Transactional(rollbackFor = Exception.class)
    public void testTransaction() {
        for (int i = 0; i <= 3; i++) {
            OrderDO orderDO = new OrderDO();
            orderDO.setDeviceId(i);
            orderDO.setOrderId("test");
            if (i == 1) {
                int test = 7 / 0;
            }
            orderMapper.insertOrder(orderDO);
        }
    }

 

@Transactional参数

 
属性 类型 描述
value String 可选的限定描述符,指定使用的事务管理器
propagation enum: Propagation 可选的事务传播行为设置
isolation enum: Isolation 可选的事务隔离级别设置
readOnly boolean 读写或只读事务,默认读写
timeout int (in seconds granularity) 事务超时时间设置
rollbackFor Class对象数组,必须继承自Throwable 导致事务回滚的异常类数组
rollbackForClassName 类名数组,必须继承自Throwable 导致事务回滚的异常类名字数组
noRollbackFor Class对象数组,必须继承自Throwable 不会导致事务回滚的异常类数组
noRollbackForClassName 类名数组,必须继承自Throwable 不会导致事务回滚的异常类名字数组

 

 

申明式事务管理:

 

 <!-- 配置事务管理器 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置基于注解的声明式事务 -->
    <!--<tx:annotation-driven transaction-manager="transactionManager" />-->


    <!-- 拦截器方式配置事物 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="append*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="repair" propagation="REQUIRED" />
            <tx:method name="delAndRepair" propagation="REQUIRED" />

            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="load*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="search*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="datagrid*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>

            <tx:method name="*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>
    <!-- 在切面上找到切点,在切点上织入通知 -->
    <aop:config>
        <!--execution(* com.service.*.*(..))   该表达式与下同效-->
        <aop:pointcut id="transactionPointcut" expression="execution(* com.service..*Impl.*(..))" />
        <aop:advisor pointcut-ref="transactionPointcut"
                     advice-ref="transactionAdvice" />
    </aop:config>

 

 

用法:

将注释去掉使之产生异常用于测试事务

 

     public void insertTransTest(List list) {
        for (int i = 0; i < list.size(); i++) {
            if(i==0){
                dailyMapper.insertDaily(list.get(i));
            }
//            else if(i==2){
//                int i1 = 1/0;
//            }
            else{
                dailyMapper.updateDaily(list.get(i));
            }
        }
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM