Spring-boot + JDBC + HSQLDB:如何验证Spring Boot是否正在使用连接池? [英] Spring-boot + JDBC + HSQLDB: How to Verify if Spring Boot is using a Connection Pool?

查看:401
本文介绍了Spring-boot + JDBC + HSQLDB:如何验证Spring Boot是否正在使用连接池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此文档:

29.1.1嵌入式数据库支持

Spring Boot可以自动配置嵌入式H2,HSQL和Derby数据库. 您无需提供任何连接网址,只需添加一个内部版本 对要使用的嵌入式数据库的依赖.

29.1.2连接到生产数据库

还可以使用池自动配置生产数据库连接 数据源.

数据源配置由外部配置控制 spring.datasource.*中的属性.例如,您可以声明 application.properties中的以下部分:

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 

[提示]您 自春季启动以来,通常不需要指定驱动程序类名称 可以从url推断出适用于大多数数据库.

[注意]对于池 要创建的数据源,我们需要能够验证是否有效 驱动程序类可用,因此我们在进行任何操作之前都会进行检查. IE.如果您设置 spring.datasource.driver-class-name = com.mysql.jdbc.Driver ,然后 类必须是可加载的.


如果在我的 application.properties 文件中放置了以下内容,该怎么办:

spring.datasource.url=jdbc:hsqldb:file:db/organization-db
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver

由于我指定了 spring.datasource.driver-class-name ,Spring Boot会自动配置池数据源吗?
还是只为嵌入式数据库驱动程序创建一个数据源而没有连接池?
如何确认Spring Boot是否正在使用连接池?

我的理解是,只要在classpath上有受支持的数据源类,spring-boot就会使用它,如果未指定,则使用tomcat作为首选项.

受支持的数据源列表在this documentation:

29.1.1 Embedded Database Support

Spring Boot can auto-configure embedded H2, HSQL and Derby databases. You don’t need to provide any connection URLs, simply include a build dependency to the embedded database that you want to use.

and

29.1.2 Connection to a production database

Production database connections can also be auto-configured using a pooling DataSource.

DataSource configuration is controlled by external configuration properties in spring.datasource.*. For example, you might declare the following section in application.properties:

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 

[Tip] You often won’t need to specify the driver-class-name since Spring boot can deduce it for most databases from the url.

[Note] For a pooling DataSource to be created we need to be able to verify that a valid Driver class is available, so we check for that before doing anything. I.e. if you set spring.datasource.driver-class-name=com.mysql.jdbc.Driver then that class has to be loadable.


What if I placed the following in my application.properties file:

spring.datasource.url=jdbc:hsqldb:file:db/organization-db
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver

Will Spring Boot auto-configure a pooling Datasource, since I specified the spring.datasource.driver-class-name?
Or will it just create a Datasource for the embedded Database driver without connection pooling?
How do I confirm if Spring Boot is using connection pooling?

解决方案

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

这篇关于Spring-boot + JDBC + HSQLDB:如何验证Spring Boot是否正在使用连接池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆