Search code examples
springspring-boothikaricp

Spring Boot 2 with pgBouncer pooling


I have a PostgreSQL 11 server with pgBouncer pooling set up and enabled.

I'd like to use its pooling mechanism rather than default Hikari and Tomcat pooling that come with the Spring Boot's spring-boot-starter-data-jpa. I've disabled HikariCP and tomcat-jdbc from the project however, I'm not sure what I'd need to set up further in order to launch the Spring app successfully.

I guess my question is how to set up a Spring application so that it doesn't use any pooling mechanism to communicate with the db as it will be handled by pgBouncer on the db side?

I have looked at a variety of questions and answers to somewhat similar questions which led to me disabling HikariCP to start with. However, I was unable to find a concise tutorial/example of how I could make this work in my case scenario.

Any help would be really appreciated.


Solution

  • Turns out I needed to define a DataSource, so I created a configuration class like this:

    @Configuration
    @ConfigurationProperties("spring.datasource")
    public class DatabaseConfig {
    
        @Value("${spring.datasource.url}")
        private String uri;
    
        @Value("${spring.datasource.username}")
        private String username;
    
        @Value("${spring.datasource.password}")
        private String password;
    
        @Bean
        public DataSource dataSource() throws SQLException {
            SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
            dataSource.setDriver(DriverManager.getDriver(uri));
            dataSource.setUrl(uri);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            return dataSource;
        }
    }