Spring-boot + JDBC + HSQLDB:如何验证 Spring Boot 是否正在使用连接池?
Spring-boot + JDBC + HSQLDB: How to Verify if Spring Boot is using a Connection Pool?
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.
和
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.
如果我将以下内容放入 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 启动会自动配置一个池数据源吗?
或者它只是为嵌入式数据库驱动程序创建一个没有连接池的数据源?
我如何确认 Spring Boot 是否正在使用连接池?
我的理解是,只要 class 路径上有受支持的数据源 class spring-boot 就会使用它,首选 tomcat如果指定 none。
支持的数据源列表在 DataSourceBuilder 中给出,目前是 tomcat、hikari、dbcp 和 dbcp2。
您可以通过从应用程序上下文中查找 javax.sql.Datasource
实现来验证是否已创建,但我不明白为什么不这样做。
感谢戴夫的回答。我刚刚开始学习 Spring 框架,所以我正在修改它。这是我在 MyApplication.main 方法中所做的,以确认 Spring Boot 是否正在使用连接池:
ApplicationContext context = SpringApplication.run(MyApplication.class);
DataSource dataSource = context.getBean(javax.sql.DataSource.class);
System.out.println("DATASOURCE = " + dataSource);
我得到了以下输出:
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; }
我还尝试了 application.properties 文件和我的 build.grade 文件的不同配置,以确认是否 Spring Boot 在自动配置 DataSource 时仍会使用连接池,我发现 Spring Boot 的自动配置总是创建一个池 DataSource。
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.
和
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.
如果我将以下内容放入 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 启动会自动配置一个池数据源吗?
或者它只是为嵌入式数据库驱动程序创建一个没有连接池的数据源?
我如何确认 Spring Boot 是否正在使用连接池?
我的理解是,只要 class 路径上有受支持的数据源 class spring-boot 就会使用它,首选 tomcat如果指定 none。
支持的数据源列表在 DataSourceBuilder 中给出,目前是 tomcat、hikari、dbcp 和 dbcp2。
您可以通过从应用程序上下文中查找 javax.sql.Datasource
实现来验证是否已创建,但我不明白为什么不这样做。
感谢戴夫的回答。我刚刚开始学习 Spring 框架,所以我正在修改它。这是我在 MyApplication.main 方法中所做的,以确认 Spring Boot 是否正在使用连接池:
ApplicationContext context = SpringApplication.run(MyApplication.class);
DataSource dataSource = context.getBean(javax.sql.DataSource.class);
System.out.println("DATASOURCE = " + dataSource);
我得到了以下输出:
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; }
我还尝试了 application.properties 文件和我的 build.grade 文件的不同配置,以确认是否 Spring Boot 在自动配置 DataSource 时仍会使用连接池,我发现 Spring Boot 的自动配置总是创建一个池 DataSource。