• SpringBoot之解决整合多数据源多事物注解问题


    SpringBoot之解决整合多数据源多事物注解问题

    概念:

      因为整合了多数据源,就代表我的项目中存在多个事物管理器,这样就不能直接使用@Transactional

    修改代码:

    修改user2接口 模拟异常,查看事物是否回滚,添加@Transactional

     启动测试:

    访问user2接口报错

    2021-01-28 12:36:39,975 [http-nio-8082-exec-3] ERROR (DirectJDKLog.java:175)- Servlet.service() for servlet [dispatcherServlet]
     in context with path [] threw exception [Request processing failed; nested exception is 
    org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 
    'org.springframework.transaction.TransactionManager' available: expected single matching bean but found 2: 
    springbootTransactionManager,springbootdtsTransactionManager] with root cause
    org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.transaction.TransactionManager'
    available: expected single matching bean but found 2: springbootTransactionManager,springbootdtsTransactionManager

    因为我们直接使用的@Transactional但是在Spring的事物管理器中却有两个事物管理器,这时他就不知道使用哪个了

    这里说一下,在Springboot2之后默认就是开始事物的不用单独写@EnableTransactionManager了

    解决:

      在事物的注解中指定事物管理器

    指定为springbootTransactionManager

     再次测试,清空数据库,先测试一下错误的,把age输入为0,看会不会插入数据

     都断出现除以0的错误

     查看数据库有没有数据

    刷新后并没有数据,事物控制成功

     尝试输入正常值

     后端没有报错,数据库成功插入数据

     到此,多数据源单事物问题解决完成

    作者:彼岸舞

    时间:2021128

    内容关于:SpringBoot

    本文来源于网络,只做技术分享,一概不负任何责任 

  • 相关阅读:
    [我的阿里云服务器] —— FTP配置
    [我的阿里云服务器] —— WorkPress
    现阶段状态,三年的门槛!!!
    dubbo初体验
    SpringBoot简易搭建
    javascript数组特性
    linux修改root账户的用户名所得的教训
    记一次虚拟机无法访问主机的坑
    javascript真假(true/false)值
    Java集合基本概念及元素添加
  • 原文地址:https://www.cnblogs.com/flower-dance/p/14339196.html
Copyright © 2020-2023  润新知