zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Net Core 使用外部登陆提供程序登陆的流程,以及身份认证的流程
    Xamarin开发手机聊天程序
    线上处理数据转载
    Logstash
    Webpack按需加载一切皆模块
    网络编程
    NET Core 事件总线
    Using INSERT IGNORE with MySQL to prevent duplicate key errors
    Spring的两种任务调度Scheduled和Async
    Embedded servlet containers
  • 原文地址:https://www.cnblogs.com/subendong/p/9272062.html
Copyright © 2011-2022 走看看