java 使用 AbstractRoutingDataSource 动态更改数据库模式/目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7960397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:12:51  来源:igfitidea点击:

Using AbstractRoutingDataSource to dynamically change the database schema/catalog

javadatabasespringruntimedatasource

提问by Carlos

According to this article, you can use the AbstractRoutingDataSource from Spring Framework to dynamically change the data source used by the application.

根据这篇文章,您可以使用 Spring Framework 中的 AbstractRoutingDataSource 来动态更改应用程序使用的数据源。

However, the data sources used are defined by configuration, instead of programmatically. Is there a way of configuring the data sources to be used at runtime?

但是,使用的数据源是由配置定义的,而不是以编程方式定义的。有没有办法配置要在运行时使用的数据源?

How scalable is this solution, i.e., what are the limitations in number of data sources?

该解决方案的可扩展性如何,即数据源数量有哪些限制?

Thanks!

谢谢!

采纳答案by Michael W

I have implemented this approach for 30 datasources and they are currently running in a Production environment without any issues. If you are using JPA you can have a look at my config:

我已经为 30 个数据源实现了这种方法,它们目前在生产环境中运行,没有任何问题。如果您使用的是 JPA,您可以查看我的配置:

dynamic-datasource-routing

动态数据源路由