如何在spring jpa 中管理数据库连接池?
How to manage database connection pool in spring jpa?
我在我的 Web 应用程序中使用 spring-boot 并使用 spring-jpa 到 read/write from/to 我的数据库。它工作得很好,但我想了解如何管理数据库连接。下面是我的数据库属性配置:
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
spring.datasource.username=user
spring.datasource.password=pwd
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-active=500
我已将最大连接数设置为 500。当用户在我的 spring 应用程序上发出请求时,将为他打开一个数据库连接。请求完成后,springjpa会关闭这个连接吗?如果没有,它什么时候关闭未使用的连接?
我已经通读了 http://docs.spring.io/spring-data/jpa/docs/current/reference/html/ 中的 spring jpa 参考文档。但它没有提及任何有关连接的信息。
使用数据库连接池时,调用 sqlconnection.close()
不一定会关闭与数据库的重量级连接,通常只会释放连接以在池中重新使用。这就是为什么建议在利用客户端连接池时尽快调用 close()
on connection 的原因。
在您的配置中,池将包含最多 500 个连接(最好配置 maxIdle
、minIdle
和 minEvictableIdleTimeMillis
来调整连接数即用型连接以及不使用时释放它们的频率)。
更多文档here
您已经发现可以从 application.properties
配置它
您可以找到所有可能的属性 here.
请注意,从 Spring Boot 1.4 开始,spring 集成的每个数据源供应商都有开箱即用的数据源属性。有spring.datasource.dbcp.*
、spring.datasource.tomcat.*
等。参见 1.4 docs
如果这还不够,并且您需要非常具体的东西,您可以自己声明数据源 bean。这是 Tomcat 数据源的示例:
@Bean
public DataSource dataSource(){
PoolProperties p = new PoolProperties();
p.setUrl("jdbc:mysql://localhost:3306/mysql");
p.setDriverClassName("com.mysql.jdbc.Driver");
p.setUsername("root");
p.setPassword("password");
p.setJmxEnabled(true);
p.setTestWhileIdle(false);
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT 1");
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(100);
p.setInitialSize(10);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(10);
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
p.setJdbcInterceptors(
"org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
"org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
DataSource datasource = new DataSource();
datasource.setPoolProperties(p);
return datasource ;
}
我在我的 Web 应用程序中使用 spring-boot 并使用 spring-jpa 到 read/write from/to 我的数据库。它工作得很好,但我想了解如何管理数据库连接。下面是我的数据库属性配置:
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
spring.datasource.username=user
spring.datasource.password=pwd
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-active=500
我已将最大连接数设置为 500。当用户在我的 spring 应用程序上发出请求时,将为他打开一个数据库连接。请求完成后,springjpa会关闭这个连接吗?如果没有,它什么时候关闭未使用的连接?
我已经通读了 http://docs.spring.io/spring-data/jpa/docs/current/reference/html/ 中的 spring jpa 参考文档。但它没有提及任何有关连接的信息。
使用数据库连接池时,调用 sqlconnection.close()
不一定会关闭与数据库的重量级连接,通常只会释放连接以在池中重新使用。这就是为什么建议在利用客户端连接池时尽快调用 close()
on connection 的原因。
在您的配置中,池将包含最多 500 个连接(最好配置 maxIdle
、minIdle
和 minEvictableIdleTimeMillis
来调整连接数即用型连接以及不使用时释放它们的频率)。
更多文档here
您已经发现可以从 application.properties
配置它
您可以找到所有可能的属性 here.
请注意,从 Spring Boot 1.4 开始,spring 集成的每个数据源供应商都有开箱即用的数据源属性。有spring.datasource.dbcp.*
、spring.datasource.tomcat.*
等。参见 1.4 docs
如果这还不够,并且您需要非常具体的东西,您可以自己声明数据源 bean。这是 Tomcat 数据源的示例:
@Bean
public DataSource dataSource(){
PoolProperties p = new PoolProperties();
p.setUrl("jdbc:mysql://localhost:3306/mysql");
p.setDriverClassName("com.mysql.jdbc.Driver");
p.setUsername("root");
p.setPassword("password");
p.setJmxEnabled(true);
p.setTestWhileIdle(false);
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT 1");
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(100);
p.setInitialSize(10);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(10);
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
p.setJdbcInterceptors(
"org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
"org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
DataSource datasource = new DataSource();
datasource.setPoolProperties(p);
return datasource ;
}