简体   繁体   English

具有在运行时创建的多个数据源的Spring Boot和Spring Data应用程序

[英]Spring Boot and Spring Data application with multiple DataSources created in runtime

I am developing a Spring Boot application that uses Spring Data JPA and will need to connect to many different databases eg PostreSQL, MySQL, MS-SQL, MongoDB. 我正在开发一个使用Spring Data JPA的Spring Boot应用程序,它将需要连接到许多不同的数据库,例如PostreSQL,MySQL,MS-SQL,MongoDB。 I need to create all datasources in runtime ie user choose these data by GUI in started application: -driver(one of the list), -source, -port, -username, -password. 我需要在运行时创建所有数据源,即用户在启动的应用程序中通过GUI选择这些数据:-driver(列表之一),-source,-port,-username,-password。 And after all he writes native sql to choosen database and get results. 毕竟,他编写了本地sql来选择数据库并获得结果。 I read a lot of things about it in stack and spring forums(eg AbstractRoutingDataSource) but all of these tutorials show how to create datasources from xml configuration or static definition in java bean. 我在堆栈和春季论坛(例如AbstractRoutingDataSource)中阅读了很多有关它的内容,但是所有这些教程都展示了如何从xml配置或java bean中的静态定义创建数据源。 It is possible to create many datsources in runtime? 是否可以在运行时创建许多数据源? How to manage transactions and how to create many sessionFactories? 如何管理交易以及如何创建许多sessionFactories? It is possible to use @Transactional annotation? 可以使用@Transactional注释吗? What is the best method to do this? 最好的方法是什么? Can someone explain me how to do this 'step by step'? 有人可以解释一下我如何“逐步”执行此操作吗?

Hope it's not too late for an answer ;) 希望答案还为时不晚;)

I developed a module which can be easily integrated in any spring project. 我开发了一个可以轻松集成到任何春季项目中的模块。 It uses a meta-datasource to hold the tenant-datasource connection details. 它使用元数据源来保存租户数据源连接详细信息。 For the tenant-datasource an AbstractRoutingDataSource is used. 对于租户数据源,使用AbstractRoutingDataSource。

Here you find my core implementation using the AbstractRoutingDataSource. 在这里,您可以找到使用AbstractRoutingDataSource的核心实现。 https://github.com/Dactabird/multitenancy https://github.com/Dactabird/multitenancy

Here is an example to show how to integrate it. 这是显示如何集成它的示例。 https://github.com/Dactabird/multitenancy-sample In this example I'm using H2 embedded db. https://github.com/Dactabird/multitenancy-sample在此示例中,我正在使用H2嵌入式数据库。 But of course you can use whatever you want. 但是当然您可以使用任何您想要的东西。

Feel free to modify it for your purposes or to ask if questions are left! 可以根据您的目的随意修改它,或者询问是否还有问题!

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

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