[Spring] and how to configure database connection pool in Spring

What is the database connection pool:

Definitions : database connection pool (Database Connection Pooling): database connection pool is a piece of memory in the computer's memory, which stores a number of already created a database connection object. Responsible for creating database connection pool database connections, is also responsible for the distribution, management and release of the database connection.
Principle :

  • Create a connection object when a certain number of initialization;
  • When an application needs to access the database from the database connection pool to get a good connection object is created, the application end of the visit, the release of the connection object back to the idle state waiting for the next to be used;
  • If the application requests linked object, the connection object has been created are in use, the database connection pool will create a new connection object to the user;
  • If the number of connected objects in the database connection pool is equal to the maximum number of connections, then the database connection pool will not create a new connection object, the application can only wait for a free connection object;
  • Database connection pool idle time exceeds the maximum idle time of the connection object will be deleted, until the rest of the connection object minimum number of connections.

Minimum number of connections : the program will be created to initialize the number of connected objects, there will be a minimum number of database connection pool the connection object, as long as the connection object is equal to this number, even if the connection object is idle for more than the maximum idle time will not be delete.
The maximum number of connections : the capacity of the database connection pool. Even if the need to use more applications that connect objects, database connection pool will not create more than this number of connected objects.
Maximum Idle Time : the connection object also needs to occupy memory, if the idle time of the connection object database connection pool exceeds this time, it will be deleted, unless the number of connected objects in the database connection pool is equal to the minimum number of connections.

Why use a database connection pool:

Creation and release of the database connection is a very time-consuming operation, frequently conduct such operations will take a lot of performance overhead, which led to the site's response time decreases when severe may cause the server to crash. And use the database connection pool can alleviate this problem to a large extent.

What are the common connection pool:

  • The DBCP : the DBCP (the DataBase Connection the pool) pool is connected to a database connection pool item on java apache, tomcat connection pooling assembly is used; dbcp alone requires three packages: common-dbcp.jar, common-pool.jar , common -collections.jar.dbcp no automatic recovery function idle connections. Go to the official website
  • C3P0 : C3P0 is an open source jdbc connection pool that implements the data source and jndi binding norms and jdbc2 support jdbc3 standard extension. c3p0 is asynchronous operation, slow process by helping jdbc operation is complete. These operations can be extended effectively improve performance. It currently use open source project Hibernate, Spring and so on. c3p0 automatic recovery Idle connection. Note: JNDI (Java Naming and Directory Interface , Java Naming and Directory Interface) is a standard provided by the SUN Java naming system interface. Go to the official website
  • Druid : Druid is an open source database connection pool Alibaba item on open source platform, the connection pool efficient performance, when using a simple SQL statement within 10 microseconds, 30 microseconds with a complex SQL. Go to the official website
  • HikariCP : HikariCP database connection pool despite a rising star, but PK out other database connection pool technology, becoming the fastest database connection pool, SpringBoot2.0 HikariCP also been adopted as the default connection pool configuration. Go to the official website

How to configure the database connection pool Spring: A Case Study with HikariCP

Import jar package is an essential part ... no details, saying only how to configure ...

method one:

In the spring of core configuration xml configuration file:

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" lazy-init="false" destroy-method="close">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
	<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/mytest?characterEncoding=utf8"></property>
	<property name="username" value="root"></property>
	<property name="password" value="1234"></property>
	<!-- 连接只读数据库时配置为true, 保证安全 -->
	<property name="readOnly" value="false" /> 
	<!-- 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒 -->
	<property name="connectionTimeout" value="30000" /> 
	<!-- 一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟 -->
	<property name="idleTimeout" value="600000" /> 
	<!-- 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';) -->
	<property name="maxLifetime" value="1800000" /> 
	<!-- 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count) -->
	<property name="maximumPoolSize" value="15" />
</bean>
Second way:

You can write the parameters to be configured in a .properties file and read in the spring of xml core configuration file. Spring provides for this purpose a class PropertyPlaceholderConfigurer disposed PropertyPlaceholderConfigurer Spring configuration file and the file path to specify the properties for a variable location, and then read out the configuration information in the properties file can be used in the form of $ {}.
Create a directory under the src folder "config", and in which you create a file "jdbc.properties":

driverClassName=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8
username=root
password=1234
readOnly=false
connectionTimeout=30000
idleTimeout=600000
maxLifetime=1800000
maximumPoolSize=15

At this time, the spring core xml configuration file is as follows:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="location" value="classpath:config/jdbc.properties"></property>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" lazy-init="false" destroy-method="close">
	<property name="driverClassName" value="${driverClassName}"></property>
	<property name="jdbcUrl" value="${jdbcUrl}"></property>
	<property name="username" value="${username}"></property>
	<property name="password" value="${password}"></property>
	<property name="readOnly" value="${readOnly}" /> 
	<property name="connectionTimeout" value="${connectionTimeout}" /> 
	<property name="idleTimeout" value="${idleTimeout}" /> 
	<property name="maxLifetime" value="${maxLifetime}" /> 
	<property name="maximumPoolSize" value="${maximumPoolSize}" />
</bean>

PropertyPlaceholderConfigurer classes may not be used, the use of context: property-placeholder tag specifies the file path properties:

<context:property-placeholder location="classpath:config/jdbc.properties"/>
Published 128 original articles · won praise 17 · views 2732

Guess you like

Origin blog.csdn.net/qq_43705275/article/details/104201026