本文介绍了如何在spring jpa中管理数据库连接池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Web应用程序中使用spring-boot并使用spring-jpa来读取/写入我的数据库。它工作得很好,但我想了解如何管理数据库连接。下面是我对数据库的属性配置:

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:

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应用程序上发出请求,将为他打开一个数据库连接。完成请求后,春天jpa会关闭这个连接吗?如果没有,它什么时候会关闭未使用的连接?

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?

我已经阅读了来自 。但它没有提到关于连接的任何事情。

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.

推荐答案

使用数据库连接池时,调用 sqlconnection.close()不一定会关闭与数据库的重量级连接,而是通常只会将连接释放为可在池中重复使用。这就是为什么建议在利用客户端连接池时尽快调用连接上的 close()

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.

在您的配置中,池将包含最多500个连接(配置 maxIdle minIdle minEvictableIdleTimeMillis 调整即用型连接的数量以及不使用时释放它们的频率。)

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).

更多doc

这篇关于如何在spring jpa中管理数据库连接池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:48