简体   繁体   English

springboot如何动态切换不同的数据源

[英]springboot how to dynamically switch different datasource

I have the requirement like to switch the datasource at runtime. 我需要在运行时切换datasource The different database configuration data(it may be mysql, oracle, mongodb ..) is persisted in DB. 不同的数据库配置数据(可能是mysql,oracle,mongodb ..)保存在DB中。 Because I need to add datasource at any time, so I do that. 因为我需要随时添加datasource ,所以我要这样做。 Now, when I use the datasource , I will get database configuration data in the DB and create a new sqlSessionFactory again. 现在,当我使用datasource ,我将在数据库中获取数据库配置数据并再次创建一个新的sqlSessionFactory

How can I give it to Spring IOC container to deal with? 如何将其交给Spring IOC容器处理?

Looks like you need supported by Spring multitenancy. 看起来您需要Spring多租户的支持。

You need to define tenant resolver to identify which tenant you need (eg by intercepting request and get tenant from header or from token), tenant context to keep tenant (eg a ThreadLocal storage where tenant is defined for each thread) and multitenant connection provider (which read tenant from thread local and provide DataSource/connection specific for the tenant). 您需要定义租户解析器以标识所需的租户(例如,通过拦截请求并从标头或令牌中获取租户),保留租户的租户上下文(例如,为每个线程定义租户的ThreadLocal存储)和多租户连接提供程序(从本地线程读取租户并提供特定于该租户的数据源/连接)。

See the example and the example 参见示例示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM