spring-mvc - Spring 启动+JDBC+HSQLDB : How to Verify if Spring Boot is using a Connection Pool?

标签 spring-mvc jdbc spring-boot spring-jdbc embedded-database

根据 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.

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 Boot 会自动配置一个池化数据源吗?
或者它只是为嵌入式数据库驱动程序创建一个没有连接池的数据源?
如何确认 Spring Boot 是否使用连接池?

最佳答案

感谢戴夫的回答。我刚刚开始学习 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 的自动配置总是创建一个池化数据源。

关于spring-mvc - Spring 启动+JDBC+HSQLDB : How to Verify if Spring Boot is using a Connection Pool?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36570276/

相关文章:

java - 如何在 Spring Tool Suite 中重命名文件

java - ArrayList 类在离开 while 循环后丢失变量

java - 应该在哪里?放在PreparedStatement中?

java - spring-mvc 中不同类的角色

java - 如何在spring-boot中提供静态html内容页面

java - 查询中的单个添加标量

java - Spring 表单映射到数组

Java 类型映射到 Java SQL 类型 Util

java - 在Spring Boot中的requestmapping和restcontroller中发送开始和结束日期

java - 创建 Spring 应用程序上下文之前阅读 application.properties