Spring Boot Pooling数据库与多个数据库的连接

时间:2018-10-16 13:36:11

标签: java spring spring-boot connection-pooling multi-database

我正在尝试使用连接池配置多数据库应用程序。 我在我的application.properties中拒绝了数据源bean:

# Topology and Configuration Datasource.
spring.datasource.tc.url=jdbc:postgresql://10.10.10.10:5432/TopologyConfiguration
spring.datasource.tc.username=adminsitraenr
spring.datasource.tc.password=abc123
spring.datasource.tc.driver-class-name=org.postgresql.Driver

# Versions Datasource.
spring.datasource.v.url=jdbc:postgresql://10.10.10.10:5432/Versions
spring.datasource.v.username=adminsitraenr
spring.datasource.v.password=abc123
spring.datasource.v.driver-class-name=org.postgresql.Driver

# Transportation Datasource.
spring.datasource.tp.url=jdbc:postgresql://10.10.10.10:5432/TransportPlan
spring.datasource.tp.username=adminsitraenr
spring.datasource.tp.password=abc123
spring.datasource.tp.driver-class-name=org.postgresql.Driver

spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.tomcat.initial-size=2
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=15
spring.datasource.tomcat.min-idle=8
spring.datasource.tomcat.default-auto-commit=true   

然后,我创建了3个配置类来定义每个数据源:

public class TopologyConfigurationDbConfig {

  /**
   * datasourceproperties creation method
   * @return datasourceproperties
   */
  @Primary
  @Bean
  @ConfigurationProperties("spring.datasource.tc")
  public DataSourceProperties tcDSProperties() {
    return new DataSourceProperties();
  }

  /**
   * datasourceproperties creation method
   * @param tcDSProperties datasource properties
   * @return datasource
   */
  @Primary
  @Bean
  public DataSource tcDS(@Qualifier("tcDSProperties") DataSourceProperties tcDSProperties) {
    return tcDSProperties.initializeDataSourceBuilder().build();
  }

  /**
   * LocalContainerEntityManagerFactoryBean creation method
   * @param tcDS datasource
   * @param builder entity manager factory builder
   * @return localcontainterentitymanagerfactorybean
   */
  @Primary
  @Bean
  public LocalContainerEntityManagerFactoryBean tcEMFactory(@Qualifier("tcDS") DataSource tcDS,
    EntityManagerFactoryBuilder builder) {
    return builder.dataSource(tcDS).packages("com.sitraplus.router.topologyconfiguration.domain").build();
  }

  /**
   * Platform transaction manager creation method
   * @param tcEMFactory entity manager factory
   * @return platformtransactionmanager
   */
  @Primary
  @Bean
  public PlatformTransactionManager tcDSTransactionManager(@Qualifier("tcEMFactory") EntityManagerFactory tcEMFactory) {
    return new JpaTransactionManager(tcEMFactory);
  }

如您所见,我已经建立了数据源类型和其他参数来配置我的连接池,但是当我运行我的应用程序时,连接数始终是10。

提前谢谢!

0 个答案:

没有答案