Trant
Trant

Reputation: 3609

How do I use disable connection pooling in Spring boot app?

I create a datasource like this in my Application.java:

@Bean
@ConfigurationProperties("datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create().build();
}

But it returns a managed datasource with pooling. due to the particular type of db I am working with, I want to disable the pooling.

What's the easiest way to do this?

Upvotes: 6

Views: 12186

Answers (2)

purpleloop
purpleloop

Reputation: 31

Here's a solution I use, adapted it from the SpringBoot 2.5 autoconfiguration of Liquibase, whose case was discussed here : https://github.com/spring-projects/spring-boot/issues/24944 (code reference : https://github.com/wilkinsona/spring-boot/blob/gh-24944/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java)

@Bean
@ConfigurationProperties("datasource")
public DataSourceProperties dataSourceProperties() {
    return new DataSourceProperties();
}

@Bean
public DataSource dataSource(DataSourceProperties dataSourceProperties) {
    String url = dataSourceProperties.determineUrl();
    String user = dataSourceProperties.determineUsername();
    String password = dataSourceProperties.determinePassword();
    String driverClassName = DatabaseDriver.fromJdbcUrl(url).getDriverClassName();    
    return DataSourceBuilder.create().type(SimpleDriverDataSource.class).url(url).username(user)
        .password(password).driverClassName(driverClassName).build();
}

Upvotes: 3

dunni
dunni

Reputation: 44535

The DataSourceBuilder has a method called type(Class) where you can specify the class which you want to use as DataSource implementation. So in your case it can look like this:

@Bean
@ConfigurationProperties("datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create().type(SimpleDriverDataSource.class).build();
}

Upvotes: 7

Related Questions