【问题标题】:Multiple Data Source using Spring JPA使用 Spring JPA 的多个数据源
【发布时间】:2015-12-09 23:53:20
【问题描述】:

我正在使用 Spring JPA 框架构建一个独立的 Java 应用程序 工作。我可以在以下情况下访问数据库:如果我给 application.properties 文件中的数据库详细信息为

spring.datasource.url=******** spring.datasource.username=******
spring.datasource.password=******

然后它就可以正常工作了。

但我必须在同一个应用程序中创建两个数据库连接,所以, 我将名称更改如下

spring.Datasource1.url=********* spring.Datasource1.username=******
spring.Datasource1.password

spring.Datasource2.url=************ spring.Datasource2.username=****
spring.Datasource2.password=*****

然后它不工作。

你能提供解决方案吗?

我已将我的代码库上传到以下位置。 https://github.com/nagtej/MultipleDataSource

【问题讨论】:

    标签: java spring spring-mvc jpa spring-data-jpa


    【解决方案1】:

    这可能对你有帮助http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources

    此外,要连接到多个数据源,您需要手动配置DataSource、EntityManagerFactory 和JpaTransactionManager。

    为此,您可以查看位于https://github.com/spring-projects/spring-data-examples/tree/master/jpa/multiple-datasources 的代码

    另一个很好的例子分享在http://xantorohara.blogspot.com.au/2013/11/spring-boot-jdbc-with-multiple.html

    【讨论】:

    • 我建议您不要提供链接,而是提供更多正文,不鼓励仅链接的答案。
    • 非常感谢您的回答。它解决了我的问题。在这里我怀疑,当我们使用多个数据源时,存储库将如何知道它需要连接到哪个数据库。你能告诉我吗?
    猜你喜欢
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 2014-12-06
    • 2015-09-07
    • 2018-09-30
    相关资源
    最近更新 更多