This topic describes how to use the Java Database Connectivity (JDBC) Druid connection pool to connect to an AnalyticDB for MySQL cluster.

Precautions

  • When you use the Druid connection pool to connect to an AnalyticDB for MySQL cluster, we recommend that you set keepAlive to true. In this way, you can reuse connections and avoid short-lived connections.
  • Use Druid 1.1.12 or later.

Configure the Druid connection pool

 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
     <! -- jdbc_url: the endpoint of the AnalyticDB for MySQL cluster to which you want to connect. You can obtain the endpoint on the Cluster Information page of the AnalyticDB for MySQL console.-->
     <property name="url" value="${jdbc_url}" />
     <! -- jdbc_user: the account used to connect to the AnalyticDB for MySQL cluster. There are two types of accounts: privileged and standard. -->
     <property name="username" value="${jdbc_user}" />
     <! -- jdbc_password: the password of the account used to connect to the AnalyticDB for MySQL cluster. -->
     <property name="password" value="${jdbc_password}" />
     <! -- Set the initial size of the connection pool, and the minimum and maximum numbers of connections. -->
     <property name="initialSize" value="5" />
     <property name="minIdle" value="10" /> 
     <property name="maxActive" value="20" />
     <! -- Set the timeout period for obtaining a connection from the connection pool. -->
     <property name="maxWait" value="60000" />
     <! -- Set the interval for detecting idle connections to be closed. Unit: milliseconds. -->
     <property name="timeBetweenEvictionRunsMillis" value="2000" />
     <! -- Set the minimum validity period of a connection in the connection pool. Unit: milliseconds. -->
     <property name="minEvictableIdleTimeMillis" value="600000" />
     <property name="maxEvictableIdleTimeMillis" value="900000" />
     <property name="validationQuery" value="select 1" />
     <property name="testWhileIdle" value="true" />
      <! -- Specify whether to check the validity of the connection each time you obtain a connection from the connection pool. A value of true indicates that the validity of the connection is checked. A value of false indicates that the validity of the connection is not checked. -->
     <property name="testOnBorrow" value="false" />
      <! -- Specify whether to check the validity of the connection each time you return a connection to the connection pool. A value of true indicates that the validity of the connection is checked. A value of false indicates that the validity of the connection is not checked. -->
     <property name="testOnReturn" value="false" />
     <property name="keepAlive" value="true" />
     <property name="phyMaxUseCount" value="100000" />
     <! -- Set the filters to be used for monitoring statistics. -->
     <property name="filters" value="stat" /> 
 </bean>