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;
}
}