spring @Transactional注解無效


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

    <!-- 配置基於注解的聲明式事務 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
</beans>
@Override
@Transactional
public ResponseHelper insert(List<TranslateLexicon> models) {
    //do something here
}

1.確保<beans>節點包含xml的tx和aop命名空間。

2.引入DataSourceTransactionManager這個bean,並配置成可以使用注解聲明事務。

3.在添加了@Transactional注解的方法內部不能使用try{}catch{}。

我的無效原因是:在方法體內部使用了try{}catch{}。

解決辦法:把try{}catch{}刪掉就行了。

問題分析:因為@Transactional注解實際上就是一個攔截器,當@Transactional代理具體方法並執行,因為你在方法體內部使用了try{}catch{},是拿不到異常信息的,拿不到異常自然就無法回滾了。

參考文章:https://www.ibm.com/developerworks/cn/java/j-master-spring-transactional-use/index.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM