All Products
Document Center

Choose a database connection pool for an application

Last Updated: May 30, 2020

You can use a database connection pool to manage database connections in a centralized manner, so as to improve application performance and reduce database loads.

  • Reuse resources: Connections can be reused to avoid the high performance overheads caused by frequent connection creations and releases. Resource reuse can also improve system stability.

  • Improve the system response efficiency: After the connection initialization is completed, all requests can directly use the existing connections, which avoids the overheads of connection initialization and release and improves the system response efficiency.

  • Prevent connection leakage: The connection pool forcibly revokes connections based on the preset revocation policy to prevent connection resource leakage.

We recommend that you use a connection pool to connect applications and databases for service operations. For Java programs, we recommend that you use Druid connection pool. The Druid connection pool version is required to be 1.1.11 or later.

Standard Spring configuration for a Druid connection pool

  1. <bean id="dataSource" class="" init-method="init" destroy-method="close">
  2. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  3. <! -- Basic properties URL, user, and password -->
  4. <property name="url" value="jdbc:mysql://ip:port/db?autoReconnect=true&rewriteBatchedStatements=true&socketTimeout=30000&connectTimeout=3000" />
  5. <property name="username" value="root" />
  6. <property name="password" value="123456" />
  7. <! -- Configure the initial size, minimum value, and maximum value -->
  8. <property name="maxActive" value="20" />
  9. <property name="initialSize" value="3" />
  10. <property name="minIdle" value="3" />
  11. <! -- maxWait indicates the time-out period for getting the connection -->
  12. <property name="maxWait" value="60000" />
  13. <! -- timeBetweenEvictionRunsMillis indicates the interval for detecting idle connections to be closed, in milliseconds -->
  14. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  15. <! -- minEvictableIdleTimeMillis indicates the minimum idle time of a connection in the connection pool, in milliseconds-->
  16. <property name="minEvictableIdleTimeMillis" value="300000" />
  17. <! -- SQL statement used to check whether a connection is available -->
  18. <property name="validationQuery" value="select 'z' from dual" />
  19. <! -- Whether to enable idle connection detection -->
  20. <property name="testWhileIdle" value="true" />
  21. <! -- Whether to check the connection status before getting a connection -->
  22. <property name="testOnBorrow" value="false" />
  23. <! -- Whether to check the connection status before releasing a connection -->
  24. <property name="testOnReturn" value="false" />
  25. <! -- Whether to close the connection at a specified time. This parameter is not required by default. However, you can add this parameter to balance the number of connections on each DRDS server node. -->
  26. <property name="phyTimeoutMillis" value="1800000" />
  27. <! -- Whether to close the connection after a specified number of SQL executions. This parameter is not required by default. However, you can add this parameter to balance the number of connections on each DRDS server node. -->
  28. <property name="phyMaxUseCount" value="10000" />
  29. </bean>