Spring Boot How to know which connection pool is used?
By:Roy.LiuLast updated:2019-08-11
In Spring Boot, @Autowired a javax.sql.DataSource, and you will know which database connection pool is using in the current running application.
1. Test Default
Spring Boot example to print a javax.sql.DataSource
Note
Read this official Spring Boot doc – Connection to a production database, to understand the algorithm for choosing a DataSource implementations – Tomcat pooling, HikariCP, Commons DBCP and Commons DBCP2.
Read this official Spring Boot doc – Connection to a production database, to understand the algorithm for choosing a DataSource implementations – Tomcat pooling, HikariCP, Commons DBCP and Commons DBCP2.
package com.mkyong; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import javax.sql.DataSource; @SpringBootApplication public class SpringBootConsoleApplication implements CommandLineRunner { @Autowired DataSource dataSource; public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootConsoleApplication. class , args); @Override public void run(String... args) throws Exception { System.out.println( "DATASOURCE = " + dataSource); |
Output, Spring Boot is using Tomcat pooling by default.
DATASOURCE = org.apache.tomcat.jdbc.pool.DataSource @7c541c15 ... |
2. Test HikariCP
To switch to another connection pool, for example HikariCP, just exclude the default and include the HikariCP in the classpath.
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <exclusions> <exclusion> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </exclusion> </exclusions> </dependency> <!-- connection pools --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version> 2.6 . 0 </version> </dependency> |
Output
DATASOURCE = HikariDataSource (HikariPool- 1 ) |
Note
Read this example – Spring Boot JDBC + MySQL + HikariCP example
Read this example – Spring Boot JDBC + MySQL + HikariCP example
References
From:一号门
RELATED ARTICLES
- springboot 读取资源文件
- spring boot结合redis实现限流
- springboot运行中获取当前jar包路径及名称
- swagger 文件上传以及requestbody参数传递
- 一篇文章梳理spring boot 加载 spring data jpa的全过程.
- Springboot 2.3 与swagger 3.0 集成
- 简单写了一个基于spring boot mosquitto 的starter
- springboot(mvc)利用applicationEvent来解耦异步运行很实用。
- springboot2.x使用interceptor之后静态资源文件加载问题
- 在测试类中初始化spring boot2的Bean的两种方法
- Spring Boot Hello World Example Thymeleaf
- Spring Boot How to change Context Path
- Spring Boot Hello World Example Thymeleaf
- Spring Boot How to change Context Path
- Spring Boot How to change Context Path
- Spring Boot How to change Tomcat port
- Spring Boot Jetty as embedded server
- Spring Boot Which main class to start
- Intellij IDEA – Spring boot reload static file is not working
- Spring Boot SLF4j Logback example
COMMENTS