0

I have a spring boot application which is not a web application. In this application i have configured embedded tomcat with the help of following bean.

@Bean public TomcatServletWebServerFactory tomcatFactory() {

    return new TomcatServletWebServerFactory() {

        protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatWebServer(tomcat);
        }

        protected void postProcessContext(Context context) {
            ContextResource contextResource = new ContextResource();
            contextResource.setName("jdbc/BPMDB");
            contextResource.setType(DataSource.class.getName());
            contextResource.setProperty("driverClassName", env.getProperty("bpm.db.driverClassName"));
            contextResource.setProperty("url", env.getProperty("bpm.db.url"));
            contextResource.setProperty("username", env.getProperty("bpm.db.username"));
            contextResource.setProperty("password", env.getProperty("bpm.db.password"));
            context.getNamingResources().addResource(contextResource);
        }
    };

}

How do i do connection pooling for this embedded tomcat. I am using spring boot 2.x which says hikaricp is the default connection pooling but how to set it into this embedded tomcat. Does this require to set properties like spring.datasource.hikari.initial-size=15 spring.datasource.hikari.max-wait=20000

but again how boot will know and how will i know that these properties are used.

Thanks.

Siddharth Singh
  • 65
  • 2
  • 12
  • Ditch this bean method and you are basically done. Rename the properties from `bpm.db` to `spring.datasource` and you are done. – M. Deinum Aug 28 '19 at 10:12
  • @M.Deinum so will the embedded server will run without this bean? – Siddharth Singh Aug 28 '19 at 10:22
  • Ofcourse it will, why would I else mention this. I strongly suggest a read of the Spring Boot reference guide and/or some of the guides/tutorials in the Spring.io website to learn spring boot. – M. Deinum Aug 28 '19 at 10:23
  • availability of HikariCP is based on the dependency, hopefully, you have used either of these two starters spring-boot-starter-jdbc or spring-boot-starter-data-jpa. Auto-configuration is always unless you define your own data source bean. – Niraj Jha Aug 28 '19 at 10:28
  • @NirajJha yes i am using spring-boot-starter-data-jpa. Will this enable the connection pooling with hikari? – Siddharth Singh Aug 28 '19 at 10:33
  • All you need is spring.datasource.url , spring.datasource.username and spring.datasource.password.Give the values in application.properties. For additional settings use spring.datasource.hikari.* properties. – Niraj Jha Aug 28 '19 at 10:48
  • To check whether hikaricp is being used and with what configuration enable the /actuator/metrics and see all the details navigating to the desired properties. – Niraj Jha Aug 28 '19 at 11:05

1 Answers1

0

I have got answer for my problem.

Its simple. We just have to make a DataSource reference and autowire it and mention database related properties along with hikari related properties.

Code is below.

@Autowired
public DataSource dataSource; 

Add above to your @Configuration marked class and add following properties to application.properties file.

spring.datasource.driver-class=...
spring.datasource.url=jdbc:oracle:thin:....
spring.datasource.username=..
spring.datasource.password=..

spring.datasource.hikari.initial-size=15
spring.datasource.hikari.max-wait=20000
spring.datasource.hikari.max-active=50
spring.datasource.hikari.max-idle=50
spring.datasource.hikari.min-idle=8

Also i have written a test case to check for hikari connection pool. Below is the code.

import javax.sql.DataSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(
    properties = "spring.datasource.type=com.zaxxer.hikari.HikariDataSource",
    classes = {ApplicationConfiguration.class,PersistenceJpaContext.class}
)
public class HikariConnectionPoolTest {


    @Autowired
    private DataSource dataSource;

    @Test
    public void hikariConnectionPoolIsConfigured() {
        assertEquals("com.zaxxer.hikari.HikariDataSource", dataSource.getClass().getName());
    }
}
Siddharth Singh
  • 65
  • 2
  • 12