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