I am configuring two data sources, and trying to set pooling properties and as per docs I should use spring.datasource.tomcat.*, that doesn't seem to work with the configuration that I am doing. What am I doing wrong? or what am I missing?
My application.properties :
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.validationQuery=SELECT 1
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username
spring.datasource.password
spring.read.datasource.tomcat.test-on-borrow=true
spring.read.datasource.tomcat.validationQuery=SELECT 1
spring.read.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.read.datasource.username
spring.read.datasource.password
Below is my configuration class : I have a similar one for read data source (for different repo/entities)
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory",
transactionManagerRef = "transactionManager",
basePackages = "com.test.feature.repo.internal")
public class DataSourceConfig {
@Primary
@Bean("dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource(){
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name="entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("dataSource")DataSource dataSource){
return builder.dataSource(dataSource).packages("com.test.feature.entity.internal").persistenceUnit("defaultPersistenceUnit").build();
}
@Primary
@Bean(name="transactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory){
return new JpaTransactionManager(entityManagerFactory);
}
}
If I try to use spring.datasource.test-on-borrow=true, then this works.
I really want to know why the .tomcat.* style doesn't work? And what I can I do to make that work?
Even if someone redirects me to some helpful reading material for understanding this, I will be glad. :)
That documentation is about the auto-configuration and you're not using it. If you are writing custom code to setup the DataSource
, you are in charge of the binding of the configuration as well.
Your code above has a @ConfigurationPropeties("spring.datasource")
. If you remove that, none of the spring.datasource.*
properties would be taken into account in your own code.
This section of the doc explains the difference between basic properties (spring.datasource
) and data source binding (spring.datasource.xyz.*
).
Regardless, if you are creating the DataSource
yourself (why?) then use a separate namespace. Reusing the spring.datasource
namespace is quite confusing as a user is expecting that the features the auto-configuration provides will be honoured. And they won't since you're writing your own config.
I have added following two lines in my application.properties
.
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.read.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
It is working...Thank you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With