All Products
Search
Document Center

ApsaraDB RDS:Use Druid to connect to an ApsaraDB RDS for MySQL instance

Last Updated:Nov 04, 2024

If your application uses the Java programming language and connections such as short-lived connections are frequently established between your application and an ApsaraDB RDS for MySQL instance or the maximum number of allowed connections to the RDS instance is reached, you can use Druid to connect to your RDS instance. Druid is a connection pool in the Java programming language. Druid helps reduce the frequency at which your application connects to the RDS instance and lower the main thread overhead of the RDS instance. This topic describes how to use Druid to connect to an RDS instance.

Prerequisites

  • Java Development Kit (JDK) 1.8 or later is installed on your application server.

  • The IP address of the server is added to an IP address whitelist of the RDS instance. For more information, see Configure an IP address whitelist.

    Note

    If your application is deployed on an Elastic Compute Service (ECS) instance that resides in the same region and virtual private cloud (VPC) as the RDS instance, you do not need to configure an IP address whitelist.

Preparations

The following section uses a Maven project as an example to describe the preparations before you use Druid to connect to an RDS instance.

Note

Apache Maven is a Java project management tool that provides developers with a standardized Java project construction process and dependency management mechanism. For more information, see Welcome to Apache Maven. A Maven-based Java project has a standardized directory structure. The pom.xml file is the project description file, the src/main/java directory stores the Java source code of the project, and the src/main/resources directory stores the project resource files.

  • Install Druid: Add the following dependency to the dependencies section in the pom.xml file. We recommend that you use Druid 1.2.13 or later.

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.19</version>
    </dependency>
  • If you want to use druid-spring-boot-starter in the project, you must exclude the Druid dependency on which druid-spring-boot-starter depends from the dependencies section in the pom.xml file and then explicitly add the Druid dependency back. Sample code:

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.2.19</version>
        <exclusions>
            <exclusion>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.19</version>
    </dependency>

Use Druid to connect to an RDS instance

  1. Configure the parameters of the connection pool: Create a file named druid.properties in the src/main/resources directory of the project and add the following content to the file to configure the parameters for the Druid connection pool. For more information about the parameters, see Common parameters of the connection pool.

    Note

    For more information about how to obtain the endpoint and port number of an RDS instance, see View and manage instance endpoints and ports.

    # The name of the driver class. You do not need to modify this parameter.
    driverClassName=com.mysql.jdbc.Driver
    
    # Specify the url, username, and password parameters based on your business requirements.
    url=jdbc:mysql://rm-bp**************.mysql.rds.aliyuncs.com:3306/database
    username=****
    password=****
    
    # Specify the number of connections that are established during initialization.
    initialSize=20
    # Specify the minimum number of idle connections.
    minIdle=20
    # Specify the maximum number of active connections in a connection pool.
    maxActive=100
    # Specify the maximum period of time that a physical connection can be reused.
    phyTimeoutMillis=3600000
    # Specify the maximum waiting time to obtain a connection. Unit: milliseconds. If no available connections exists in the connection pool and the maximum number of connections is reached, the request from the application to obtain a connection is blocked until the maximum waiting time elapses. We recommend that you retain the parameter setting.
    maxWait=5000
    # Specify the timeout period for establishing a TCP connection between the database driver and the server on which the RDS instance resides. Unit: milliseconds.
    connectTimeout=20000
    # Specify the socket timeout period for an application that is waiting for a response after the application sends data, such as SQL statements, over a TCP connection. Unit: milliseconds.
    socketTimeout=60000
    
    # Configure the required parameters to verify connections. We recommend that you retain the parameter settings.
    testWhileIdle=true
    testOnBorrow=false
    testOnReturn=false
    
    # Configure the parameters related to caching. The PreparedStatement parameter is set to true, which indicates that caching is enabled. We recommend that you retain the parameter settings.
    poolPreparedStatements=true
    maxPoolPreparedStatementPerConnectionSize=100
  2. Use the Druid connection pool to connect to an RDS instance: Import the required dependencies into the source code file in the src/main/java directory, build the DruidPoolDemo class, obtain information about the Java Database Connectivity (JDBC) connections from the Druid connection pool, and then access the RDS instance.

    // Import the required dependencies.
    package com.aliyun.rdsfinops.collector.impl;
    import com.alibaba.druid.pool.DruidDataSourceFactory;
    import javax.sql.DataSource;
    import java.io.InputStream;
    import java.sql.*;
    import java.util.Properties;
    
    public class DruidPoolDemo {
        private static DataSource dataSource = null;
        String tableName = "sql_table_test";
        // Load parameter settings.
        static {
            Properties properties = new Properties();
            // Obtain the configurations of the connection pool.
            InputStream inputStream = DruidPoolDemo.class.getClassLoader().getResourceAsStream("druid.properties");
            try {
                properties.load(inputStream);
                // Initialize the connection pool.
                dataSource = DruidDataSourceFactory.createDataSource(properties);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        /* 
          View subsequent database operations in the following sections:
          Create a table: public void createTable()
          Insert data: public void insertData()
          Query data: public void selectData()
          Delete data: public void deleteData()
        */
    }

Common parameters of the connection pool

When you use Druid to connect to an RDS instance, we recommend that you configure the following parameters for the connection pool to ensure that the RDS instance runs in a stable and efficient manner.

Important

To minimize potential risks and uncertainties and ensure system stability and reliability, we recommend that you perform a complete feature and performance testing before you apply the new parameter values in your production environment.

Recommended configurations

When you use the Druid connection pool, we recommend that you configure parameters to reduce database runtime risks. The following table describes the parameters.

Parameter

Description

Default value

Recommended value

Remarks

initialSize

The number of connections established during the initialization of the connection pool.

0

20 to 80

  • Specify an appropriate value that allows the application to handle concurrent requests immediately after the application starts.

  • We recommend that you set the parameter to a value between 60% and 80% of the average number of concurrent connections.

minIdle

The minimum number of idle connections.

0

20 to 80

  • Specify an appropriate value that allows you to reserve a specific number of connections to quickly respond to database request bursts.

  • We recommend that you configure this parameter based on the value of the initialSize parameter or set this parameter to the number of connections that the Java virtual machine (JVM) retains when the JVM is not overloaded.

  • The number of active connections, which is specified by the maxActive parameter, is not affected by this parameter.

maxActive

The maximum number of active connections in the connection pool.

8

100

  • Configure this parameter based on the actual requirements of the application and the processing capability of the database.

  • We recommend that you set this parameter to the maximum number of concurrent connections that the database can handle or to a value greater than the number of estimated concurrent connections during peak hours. You can reserve a specific number of connections to process traffic bursts.

phyTimeoutMillis

The maximum period of time that a physical connection can be reused. Unit: milliseconds.

-1

3600000 to 28800000

  • The default value -1 specifies that connections are always available for reuse.

  • Configure this parameter based on your business requirements. This parameter is used to prevent long-lived idle connections and save resources.

maxWait

The maximum waiting time to obtain a connection. Unit: milliseconds.

-1

5000

  • If no available connections exist in the connection pool and the maximum number of connections is reached, the request from the application to obtain a connection is blocked until the maximum waiting time elapses.

  • The default value -1 specifies that the application continues to wait. The value 0 specifies that the application does not wait. We recommend that you do not set this parameter to -1 or 0.

  • In most cases, the waiting time to obtain a connection is 10 to 30 milliseconds. We recommend that you configure this parameter based on your business requirements or use the recommended value.

connectTimeout

The connection timeout period. Unit: milliseconds.

10000

3000

  • Specify the timeout period for establishing a TCP connection between the database driver and the server on which the RDS instance resides.

  • We recommend that you specify a timeout period in the range of 1s to 10s when you configure a connection pool. The timeout period varies based on the network quality and the distance between the application and the server.

socketTimeout

The socket timeout period. Unit: milliseconds.

10000

10000 to 60000

  • Specify the timeout period for the application that is waiting for a response after the application sends data, such as SQL statements, over a TCP connection.

  • We recommend that you do not set this parameter to a small value. If an SQL statement is executed for a long period of time, you must check the SQL statement or the database before you modify this parameter.

testWhileIdle

Specifies whether to enable idle connection detection.

false

true

We recommend that you set this parameter to true. The connection pool periodically checks the status of idle connections.

Optional configurations

You can configure other parameters to improve database performance. The following table describes the parameters.

Parameter

Description

Default value

Recommended value

Remarks

poolPreparedStatements

Specifies whether to cache PreparedStatement objects.

true

true

  • If you set this parameter to true, you can reduce the number of times that SQL statements are compiled when the same SQL statements are repeatedly executed. This improves database performance.

  • If you set this parameter to true, the system can effectively prevent SQL injection attacks and enhance security.

  • If changes to the SQL statements are significant or a large number of large fields are queried, a large amount of memory of the JVM and database server is consumed when you set this parameter to true.

maxPoolPreparedStatementPerConnectionSize

The maximum number of PreparedStatement objects that are cached by each connection.

10

100

  • If you set this parameter to true, the connection pool limits the maximum number of PreparedStatement objects that are cached by each connection.

  • If your application executes a large number of repeated SQL statements, you can increase the value of this parameter. Do not specify an excessively large value for this parameter to avoid memory resource waste.

Default configurations

You can use the default configurations of specific parameters or modify the parameters based on your business requirements. The following table describes the parameters.

Parameter

Description

Default value

Recommended value

Remarks

failFast

The operation that is performed when an error occurs in obtaining a connection.

false

false

  • In most cases, you can retain the default value.

  • In the following scenarios in which the system is sensitive to errors and frequent failures occur in obtaining connections, you can set this parameter to true:

    • Quick response to errors: If the maximum number of connections is reached or a connection times out, the operation to obtain a connection immediately fails when an error occurs. This system does not retry or wait for the operation to complete.

    • Minimized waiting time: In high-concurrency scenarios, you can use this parameter together with the maxWaitThreadCount parameter to minimize the waiting time to obtain connections.

timeBetweenEvictionRunsMillis

The interval at which idle connections are detected. Unit: milliseconds.

60000

60000

  • The connection pool checks connections at the specified interval to identify whether the idle time of connections exceeds the minimum idle time that is specified by the minEvictableIdleTimeMillis parameter.

  • We recommend that you retain the default value. If you want to modify this parameter, modify the parameter based on your business requirements to achieve optimal connection pool utilization and performance.

Subsequent database operations

If you want to perform operations on databases, you can add user-defined functions to the DruidPoolDemo class. The following sample codes provide examples on how to create a table, insert data into the table, query data, and delete data:

Create a table

String tableName = "sql_table_test";
public void createTable() throws SQLException {
    // Create a table.
    try (Connection connection = dataSource.getConnection()) {
        try (Statement statement = connection.createStatement()) {
            String sql = "create table if not exists " + tableName + "(id VARCHAR(255), name VARCHAR(255), PRIMARY KEY (id))";
            int ret = statement.executeUpdate(sql);
            System.out.println(ret);
        }
    }
}

Insert data

String tableName = "sql_table_test";
public void insertData(){
    // Insert data to the table.
    try (Connection connection = dataSource.getConnection()) {
        String sql = "insert into " + tableName + "(id,name) values(?,?)";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, "aa");
            ps.setString(2, "bb");
            int ret = ps.executeUpdate();
            System.out.println(ret);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

Query data

String tableName = "sql_table_test";
public void selectData() throws SQLException {
    // Query data in the table.
    try (Connection connection = dataSource.getConnection()) {
        String sql = "select * from " + tableName + " where id=?";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, "aa");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                String id = rs.getString(1);
                String name = rs.getString(2);
                System.out.println("id=" + id);
                System.out.println("name=" + name);
            }
        }
    }
}

Delete data

String tableName = "sql_table_test";
public void deleteData(){
    // Delete data from the table.
    try (Connection connection = dataSource.getConnection()) {
        String sql = "delete from " + tableName + " where id=?";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, "aa");
            ps.executeUpdate();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

References