5

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?

Trant
  • 3,461
  • 6
  • 35
  • 57
  • Is the pool the issue or is multiple connections the issue? You can configure it to create a pool with 1 connection. – Gregg Feb 04 '17 at 22:09
  • sorry, keeping one connection is a problem. I need a new fresh connection each time. – Trant Feb 04 '17 at 22:12
  • 2
    Possible duplicate of [How to completely disable Connection Pooling in Spring / Tomcat?](http://stackoverflow.com/questions/33665860/how-to-completely-disable-connection-pooling-in-spring-tomcat) – Gregg Feb 04 '17 at 22:14
  • okay, but how would I change my configuration to use SimpleDriverDataSource ? – Trant Feb 04 '17 at 22:15
  • Lots of examples here. http://www.programcreek.com/java-api-examples/index.php?api=org.springframework.jdbc.datasource.SimpleDriverDataSource – Gregg Feb 04 '17 at 22:35

2 Answers2

6

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();
}
dunni
  • 43,386
  • 10
  • 104
  • 99
  • 5
    I tried your suggestion, but I've an exception: java.lang.IllegalArgumentException: Driver must not be null at org.springframework.util.Assert.notNull(Assert.java:193) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]. I set driverName on DataSourceBuilder. Some ideas? Thanks – drenda Aug 06 '18 at 20:15
  • 1
    The DataSourceBuilder method doesn't work as the default constructor for SimpleDriverDataSource does not instantiate a driver. Just use the constructors/setters of SimpleDriverDataSource and specify the driver class like `org.postgresql.Driver.class` for postgresql, etc. – rougou Mar 05 '19 at 08:11
  • @dunni Neither SimpleDriverDataSource nor DriverManagerDataSource classes provide close method. Does this mean we don't need to close the connection ? – Pramod Apr 02 '21 at 05:20
3

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();
}
purpleloop
  • 31
  • 4