Spring-boot + JDBC + HSQLDB: How to Verify if Spring Boot is using a Connection Pool?

 ̄綄美尐妖づ 提交于 2019-12-06 13:54:44

My understanding is that as long as there is a supported datasource class on the classpath spring-boot will use it, with tomcat being the preference if none is specified.

The list of supported datasources is given in DataSourceBuilder, and currently is tomcat, hikari, dbcp and dbcp2.

You could verify if one has been created by looking for javax.sql.Datasource implementations from from the application context, though I don't see why one wouldn't.

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceBuilder.java

Thanks for your answer Dave. I'm just starting to learn Spring framework so I'm tinkering with it. This is what I did in MyApplication.main method to confirm if Spring Boot is using connection pooling:

ApplicationContext context = SpringApplication.run(MyApplication.class);
DataSource dataSource = context.getBean(javax.sql.DataSource.class);
System.out.println("DATASOURCE = " + dataSource);

And I got the following output:

DATASOURCE = org.apache.tomcat.jdbc.pool.DataSource@a5b0b86{ConnectionPool[defaultAutoCommit=null; defaultReadOnly=null; defaultTransactionIsolation=-1; defaultCatalog=null; driverClassName=org.hsqldb.jdbcDriver; maxActive=100; maxIdle=100; minIdle=10; initialSize=10; maxWait=30000; testOnBorrow=false; testOnReturn=false; timeBetweenEvictionRunsMillis=5000; numTestsPerEvictionRun=0; minEvictableIdleTimeMillis=60000; testWhileIdle=false; testOnConnect=false; password=********; url=jdbc:hsqldb:mem:testdb; username=sa; validationQuery=null; validationQueryTimeout=-1; validatorClassName=null; validationInterval=30000; accessToUnderlyingConnectionAllowed=true; removeAbandoned=false; removeAbandonedTimeout=60; logAbandoned=false; connectionProperties=null; initSQL=null; jdbcInterceptors=null; jmxEnabled=true; fairQueue=true; useEquals=true; abandonWhenPercentageFull=0; maxAge=0; useLock=false; dataSource=null; dataSourceJNDI=null; suspectTimeout=0; alternateUsernameAllowed=false; commitOnReturn=false; rollbackOnReturn=false; useDisposableConnectionFacade=true; logValidationErrors=false; propagateInterruptState=false; ignoreExceptionOnPreLoad=false; }

I also tried different configurations with the application.properties file and my build.grade file to confirm if Spring Boot would still use connection pooling when it auto configures a DataSource and I found out that Spring Boot's auto-configuration always creates a pooling DataSource.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!