• 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

  • 相关阅读:
    转载:山寨币凶猛
    Windows8.1 关机异常的解决
    Windows8、Windows8.1使用便签工具
    下载Sourceforge等国内无法下载站点文件的另一种方法
    专著出版成本计算
    PL2303 Windows8.1驱动
    转载:寒门再难出贵子
    华为荣耀品牌独立,子品牌战略能否实现新突破
    路由大战前夜,盘点智能路由的前世今生
    2020年实用工具推荐
  • 原文地址:https://www.cnblogs.com/subendong/p/9272062.html
Copyright © 2020-2023  润新知