1. 程式人生 > >spring+ibatis事務配置

spring+ibatis事務配置

<!-- 事務管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
    </bean>

<!-- 宣告式事務管理 -->
    <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
        <property name="transactionManager" ref="transactionManager"></property>
        <property name="transactionAttributes">
            <props>
                <prop key="add*">PROPAGATION_REQUIRED</prop>            
                <prop key="edit*">PROPAGATION_REQUIRED</prop>
                <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>

<!-- 注意該DAO 一定要繼承SqlMapClientDaoSupport 使用getSqlMapClientTemplate()方法,並且要丟擲 throws DataAccessException 異常 spring才能捕獲並回滾 -->

Spring的transactionAttributes的配置

PROPAGATION_REQUIRED--支援當前事務,如果當前沒有事務,就新建一個事務。這是最常見的選擇。
PROPAGATION_SUPPORTS--支援當前事務,如果當前沒有事務,就以非事務方式執行。
PROPAGATION_MANDATORY--支援當前事務,如果當前沒有事務,就丟擲異常。
PROPAGATION_REQUIRES_NEW--新建事務,如果當前存在事務,把當前事務掛起。
PROPAGATION_NOT_SUPPORTED--以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。
PROPAGATION_NEVER--以非事務方式執行,如果當前存在事務,則丟擲異常。
PROPAGATION_NESTED--如果當前存在事務,則在巢狀事務內執行。如果當前沒有事務,則進行與PROPAGATION_REQUIRED類似的操作。


如果出現<prop key="myMethod">PROPAGATION_REQUIRED,readOnly,-Exception </prop>

其中:

-Exception表示有Exception丟擲時,事務回滾. -代表回滾+就代表提交

readonly 就是read only, 設定操作許可權為只讀,一般用於查詢的方法,優化作用