Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot doesn't pick spring.datasource.tomcat.* while programmatically configuring datasources?

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. :)

like image 308
Sachin Sharma Avatar asked Sep 16 '25 06:09

Sachin Sharma


2 Answers

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.

like image 69
Stephane Nicoll Avatar answered Sep 19 '25 04:09

Stephane Nicoll


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.

like image 31
kalaivani v Avatar answered Sep 19 '25 03:09

kalaivani v