Java spring jpa如何管理数据库连接池?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38672900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to manage database connection pool in spring jpa?
提问by Joey Yi Zhao
I am using spring-boot in my web application and use spring-jpa to read/write from/to my database. It works very well but I want to understand how to manage the database connections. Below is my properties configuration for database:
我在我的 Web 应用程序中使用 spring-boot 并使用 spring-jpa 从/向我的数据库读取/写入。它工作得很好,但我想了解如何管理数据库连接。以下是我的数据库属性配置:
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
I have set the maximum connections to 500. When a user makes a request on my spring application, a database connection will be opened for him. After finishing the request, will spring jpa close this connection? If not, when will it close the unused connections?
我已将最大连接数设置为 500。当用户对我的 spring 应用程序发出请求时,将为他打开一个数据库连接。请求完成后,spring jpa 会关闭这个连接吗?如果没有,它什么时候关闭未使用的连接?
I have read through the spring jpa reference document from http://docs.spring.io/spring-data/jpa/docs/current/reference/html/. But it doesn't mention anything about the connections.
我已经阅读了http://docs.spring.io/spring-data/jpa/docs/current/reference/html/ 中的 spring jpa 参考文档。但它没有提到有关连接的任何内容。
采纳答案by guido
When using DB connection pooling, a call to sqlconnection.close()
will not necessarily close the heavyweight connection to the database, instead most often will just release the connection as re-usable in the pool. That's why it is advisable to invoke the close()
on connection as soon as possible when leveraging a client side connection pool.
使用数据库连接池时,调用sqlconnection.close()
不一定会关闭与数据库的重量级连接,相反,大多数情况下只会将连接释放为可在池中重用。这就是为什么close()
在利用客户端连接池时建议尽快调用on connection的原因。
In your configuration, the pool will contain a maximum number of 500 connections ( it would be also good to configure maxIdle
, minIdle
, and minEvictableIdleTimeMillis
to tune the number of ready-to-use connections and how often to release them when not used).
在您的配置,池将包含500个连接的最大数目(这将是也是很好的配置maxIdle
,minIdle
以及minEvictableIdleTimeMillis
调整准备使用的连接的数量和频率释放他们不使用时)。
Some more doc here
这里还有一些文档
回答by Evgeni Dimitrov
You have already found that you can configure this from application.properties
You can find all the possible properties here.
您已经发现您可以从这里进行配置。application.properties
您可以在此处找到所有可能的属性。
Notice that from Spring Boot 1.4 there are datasource properties for every datasource vendor that spring integrates with, out of the box. There is spring.datasource.dbcp.*
,spring.datasource.tomcat.*
and so on. See 1.4 docs
请注意,从 Spring Boot 1.4 开始,spring 集成的每个数据源供应商都有数据源属性,开箱即用。有spring.datasource.dbcp.*
,spring.datasource.tomcat.*
等等。见1.4 文档
If that's not enought, and you need something very specific, you can declare the datasource bean yourself. Here is the example with Tomcat datasource:
如果这还不够,并且您需要非常具体的东西,您可以自己声明数据源 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 ;
}