0

I have two datsources but I am only able to load username, password and url. Other properties like poolPingEnabled, poolPingQuery, poolPingConnectionsNotUsedFor, poolMaximumActiveConnections.

Note: My code flow is working and I am able to connect to the two DBs. My main issue is why the other properties are not getting loaded. I couldn't find any example which helps to add the properties I mentioned above.

@Configuration
    public class DatabaseConfiguration {
  public static final String GDPR_DATASOURCE = "gdpr";
  public static final String CONFIG_DATASOURCE = "config";

  @Bean(name = GDPR_DATASOURCE)
  @ConfigurationProperties(prefix = "gdpr.jdbc")
  public DataSource gdprDataSource() {
    return DataSourceBuilder.create().build();
  }

  @Bean(name = CONFIG_DATASOURCE)
  @ConfigurationProperties(prefix = "config.jdbc")
  public DataSource configDataSource() {
    return DataSourceBuilder.create().build();
  }
}

Application.properties: username and password here are only examples. In my case url,username and password everything is different.

spring.datasource.initialize=false
gdpr.jdbc.url=jdbc:mysql://localhost:3306/gdpr
gdpr.jdbc.driverClassName=com.mysql.jdbc.Driver
gdpr.jdbc.username=root
gdpr.jdbc.password=local
gdpr.jdbc.poolPingEnabled=true
gdpr.jdbc.poolPingQuery=SELECT 1
gdpr.jdbc.poolPingConnectionsNotUsedFor=9000
gdpr.jdbc.poolMaximumActiveConnections=25

config.jdbc.url=jdbc:mysql://localhost:3306/config
config.jdbc.driverClassName=com.mysql.jdbc.Driver
config.jdbc.username=root
config.jdbc.password=local
config.jdbc.poolPingEnabled=true
config.jdbc.poolPingQuery=SELECT 1
config.jdbc.poolPingConnectionsNotUsedFor=10000
config.jdbc.poolMaximumActiveConnections=25

MyBatisConfiguration.java : Here I am creating two SqlSessionFactoryBean. These are being used for contacting the individual DBs.

@Configuration
public class MyBatisConfiguration {
  private static final String GDPR_SESSION_FACTORY = "gdprSessionFactory";
  private static final String CONFIG_SESSION_FACTORY = "configSessionFactory";

  /** This method is used for generating SqlSessionFactoryBean for gdpr DB.
   * @param gdprDataSource gdpr datasource with configuration properties loaded
   * @return sqlSessionFactoryBean for gdpr DB
   */
  @Bean(name = GDPR_SESSION_FACTORY, destroyMethod = "")
  @Primary
  public SqlSessionFactoryBean gdprSqlSessionFactory(
      @Named(DatabaseConfiguration.GDPR_DATASOURCE) final DataSource gdprDataSource)
  throws Exception {
    final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(gdprDataSource);
    SqlSessionFactory sqlSessionFactory;
    sqlSessionFactory = sqlSessionFactoryBean.getObject();
    sqlSessionFactory.getConfiguration().addMapper(GdprDatabaseMapper.class);
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources(
    "classpath:GdprDatabaseMapper.xml"));
    return sqlSessionFactoryBean;
  }

  /** This method creates MapperFactory for SqlSessionFactoryBean GDPR_SESSION_FACTORY.
   * @param sqlSessionFactoryBean gdpr SqlSessionFactoryBean
   * @return MapperFactoryBean for SqlSessionFactoryBean
   */
  @Bean
  public MapperFactoryBean<GdprDatabaseMapper> gdprMapper(
  @Named(GDPR_SESSION_FACTORY) final SqlSessionFactoryBean sqlSessionFactoryBean)
  throws Exception {
    MapperFactoryBean<GdprDatabaseMapper> factoryBean =
    new MapperFactoryBean<>(GdprDatabaseMapper.class);
    factoryBean.setSqlSessionFactory(sqlSessionFactoryBean.getObject());
    return factoryBean;
  }

  /** This method is used for generating SqlSessionFactoryBean for config DB.
   * @param configDataSource config datasource with configuration properties loaded
   * @return sqlSessionFactoryBean for config DB
   */
  @Bean(name = CONFIG_SESSION_FACTORY, destroyMethod = "")
  public SqlSessionFactoryBean configSqlSessionFactory(
  @Named(DatabaseConfiguration.CONFIG_DATASOURCE) final DataSource configDataSource)
  throws Exception {
    final SqlSessionFactoryBean sqlSessionFactoryBean =
    new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(configDataSource);
    final SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
    sqlSessionFactory.getConfiguration().addMapper(ConfigDatabaseMapper.class);
PathMatchingResourcePatternResolver resolver = new     PathMatchingResourcePatternResolver();
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources(
    "classpath:ConfigDatabaseMapper.xml"));
    return sqlSessionFactoryBean;
  }

  /** This method creates MapperFactory for SqlSessionFactoryBean CONFIG_SESSION_FACTORY.
   * @param sqlSessionFactoryBean config SqlSessionFactoryBean
   * @return MapperFactoryBean for SqlSessionFactoryBean
   */
  @Bean
  public MapperFactoryBean<ConfigDatabaseMapper> configMapper(
  @Named(CONFIG_SESSION_FACTORY) final SqlSessionFactoryBean sqlSessionFactoryBean)
  throws Exception {
MapperFactoryBean<ConfigDatabaseMapper> factoryBean =
    new MapperFactoryBean<>(ConfigDatabaseMapper.class);
factoryBean.setSqlSessionFactory(sqlSessionFactoryBean.getObject());
return factoryBean;
  }
}
  • One of beans should have annotation @Primary. See also: [Another stack question](https://stackoverflow.com/questions/30337582/spring-boot-configure-and-use-two-datasources) –  Apr 17 '18 at 09:15
  • I have added more details. I am using @Primary for SqlSessionFactoryBean. My code works as I need, I just want that other properties should also be loaded. – Hitesh Singh Apr 17 '18 at 10:49
  • do you have mybatis dependency? – sidgate Apr 17 '18 at 12:14
  • Yes. I have removed mybatis-config.xml file as I am loading all the properties in Datasource through application.properties. Also I am not using spring default datasaource as you can see I have set spring.datasource.initialize=false. – Hitesh Singh Apr 17 '18 at 14:35
  • what connection pool are you using? – Roman Konoval Apr 17 '18 at 14:51
  • tomcat connection pool – Hitesh Singh Apr 17 '18 at 15:23

1 Answers1

0

The problem is that you are trying to use configuration parameters that mybatis built in connection pool understands. Spring does not know about them.

Mybatis connection pool is not used when you use spring boot. Connection pool configured in spring is used instead.

To fix the issue you need to add some connection pool as described here.

Then you need to configure spring boot managed connection pool via application properties. Exact properties depend on the connection pool you using. More details are here.

For tomcat connection pool for example it looks like this:

# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.tomcat.max-wait=10000

# Maximum number of active connections that can be allocated from this pool at the same time.
spring.datasource.tomcat.max-active=50

# Validate the connection before borrowing it from the pool.
spring.datasource.tomcat.test-on-borrow=true
Roman Konoval
  • 12,545
  • 2
  • 32
  • 46