在spring数据jpa中动态实现数据源 [英] implement dynamically datasource in spring data jpa

查看:84
本文介绍了在spring数据jpa中动态实现数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有N个服务器,N个数据库和N个配置.请参阅下面的情况

I have N Servers, N DBs and N configuration. see the scenario below

因此,在每个请求上,我都需要根据配置访问服务器和数据库.

So, on every request , I need to access server and db based on configuration.

如何在spring data jpa中动态实现数据源?

How can implement dynamically data source in spring data jpa?

推荐答案

您可以尝试非常好的示例.在您的情况下,由于您的配置位于数据库而不是属性文件中,因此您需要执行额外的第一个数据库查找以获取适当的数据库配置并返回适当的数据源对象.

You can try AbstractRoutingDatasource provided by Spring since version 2.0.1. using which you can dynamically use appropriate data-source . For integration with Spring data JPA check this very good example. In your case since your configurations are in DB instead of properties file you would need to perform an extra first database lookup to get the appropriate database configuration and return appropriate data-source object.

这篇关于在spring数据jpa中动态实现数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆