简体   繁体   English

带有Spring Boot的多租户数据库配置的正确设置事务管理器

[英]Proper setting transaction manager with multitenant databases configuration with spring-boot

I have multitenant database in Spring Boot. 我在Spring Boot中有多租户数据库。 I store multi spring JDBC templates (based on tomcat Data Sources, configured manually) in map (immutable bean). 我在地图(不可变bean)中存储了多个spring JDBC模板(基于手动配置的tomcat数据源)。 And I choose proper data source based on uuid in a request (connection pool per database). 我根据请求中的uuid(每个数据库的连接池)选择合适的数据源。 I have disabled standard configuration in Spring Boot by: 我通过以下方式禁用了Spring Boot中的标准配置:

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

What is the proper way of transaction manager configuration? 事务管理器配置的正确方法是什么? With single data source I can use PlatformTransactionManager, but how it should be done with multiple jdbc templates/data sources in spring? 对于单个数据源,我可以使用PlatformTransactionManager,但是在春季如何对多个jdbc模板/数据源进行处理? It would be the best if I could set everything dynamically. 如果可以动态设置所有内容,那将是最好的。 Thanks in advance. 提前致谢。

Here a solution for using multiple datasources 这是使用多个数据源的解决方案

http://www.baeldung.com/spring-data-jpa-multiple-databases http://www.baeldung.com/spring-data-jpa-multiple-databases

Configure Two DataSources 配置两个数据源

If you need to configure multiple data sources, you can apply the same tricks that are described in the previous section. 如果需要配置多个数据源,则可以应用上一节中介绍的相同技巧。 You must, however, mark one of the DataSource @Primary as various auto-configurations down the road expect to be able to get one by type. 但是,您必须将DataSource @Primary中的一个标记为,因为将来各种自动配置都希望能够按类型获取一个。

If you create your own DataSource, the auto-configuration will back off. 如果您创建自己的数据源,则自动配置将退出。 In the example below, we provide the exact same features set than what the auto-configuration provides on the primary data source 在下面的示例中,我们提供了与自动配置在主数据源上提供的功能完全相同的功能集

@Bean
@Primary
@ConfigurationProperties("app.datasource.foo")
public DataSourceProperties fooDataSourceProperties() {
    return new DataSourceProperties();
}

@Bean
@Primary
@ConfigurationProperties("app.datasource.foo")
public DataSource fooDataSource() {
    return fooDataSourceProperties().initializeDataSourceBuilder().build();
}

@Bean
@ConfigurationProperties("app.datasource.bar")
public BasicDataSource barDataSource() {
    return (BasicDataSource) DataSourceBuilder.create()
            .type(BasicDataSource.class).build();
}

fooDataSourceProperties has to be flagged @Primary so that the database initializer feature uses your copy (should you use that). fooDataSourceProperties必须标记为@Primary,以便数据库初始化程序功能使用您的副本(您应该使用该副本)。

app.datasource.foo.type=com.zaxxer.hikari.HikariDataSource
app.datasource.foo.maximum-pool-size=30

app.datasource.bar.url=jdbc:mysql://localhost/test
app.datasource.bar.username=dbuser
app.datasource.bar.password=dbpass
app.datasource.bar.max-total=30

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

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