baomidou / dynamic-datasource

dynamic datasource for springboot 多数据源 动态数据源 主从分离 读写分离 分布式事务
https://www.kancloud.cn/tracy5546/dynamic-datasource/2264611
Apache License 2.0
4.67k stars 1.19k forks source link

使用动态切换数据源,执行过程中数据源切换,失败以后影响所有其他使用系统的用户。 #210

Closed pan-common closed 4 years ago

pan-common commented 4 years ago

Enviroment

JDK Version(required): 1.8

SpringBoot Version(required): 2.2.5.RELEASE

dynamic-datasource-spring-boot-starter Version(required): 2.2.5.RELEASE

druid Version(optional): 1.1.20

Describe

3台服务器通过nginx负载均衡,一个执行需要5秒左右的方法中做了动态切换数据源,连续点击按钮调用这个方法数据源切换之后切不回来。最终会造成所有服务都因为切换了数据源无法正常访问。 代码如下:

try {
                        if(zxjcjgEntity.getJcrq()!=null) {
                             year = DateUtil.format(zxjcjgEntity.getJcrq(), "yyyy");
                        }else  if(zxjcjgEntity.getKsrq()!=null) {
                             year = DateUtil.format(zxjcjgEntity.getKsrq(), "yyyy");
                        }
                        DynamicDataSourceContextHolder.push("jcpt_"+year);
                        return invocation.proceed();
                    }finally {
                        DynamicDataSourceContextHolder.push("master");
                    }

数据源切换是和服务绑定的还是用户当前用户打开的数据库连接? 有没有办法避免一个用户的操作使整个服务出错? Expected Result:

Actual Result:

Steps to reproduce

huayanYu commented 4 years ago

finally不应该是关闭吗?

huayanYu commented 4 years ago

你一个线程完了不清空还要往里面push,下一个进来了当然会影响