All Products
Search
Document Center

Lindorm:Tutorial: Use the Druid connection pool to connect to LindormTSDB

Last Updated:Aug 14, 2023

This topic describes how to use the Druid connection pool to connect to LindormTSDB.

Background information

When you use Java to develop an application that stores data in LindormTSDB, we recommend that you configure the application to use the JDBC driver to connect to LindormTSDB. In this case, Connection objects are created to connect to LindormTSDB. If a new Connection object is created each time LindormTSDB is connected, a large number of resources are required. To reduce the resources required for connections, you can use a connection pool to create and manage connections.

Prerequisites

  • Java Development Kit (JDK) 1.8 or later is installed.

  • The IP address of your client is added to the whitelist of the Lindorm instance. For more information, see Configure a whitelist.

Procedure

The open source connection pool Alibaba Druid is used in this topic as an example. To download Alibaba Druid, visit GitHub.

  1. Add the dependency of Alibaba Druid to a Maven project. Start the client, create a Maven project, and add the following dependency to the pom.xml file:

    <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.2.6</version>
        </dependency>
  2. You can use one of the following methods to install the JDBC driver for Lindorm:

    • Manually install the JDBC driver

      Download the Lindorm-all-client JAR package to your client and install the JDBC driver. Select the JDBC driver version that you want to install. For example, if you want to install the JDBC driver 2.1.5, download the lindorm-all-client-2.1.5.jar package.

    • Use Maven to download the JDBC driver

      To integrate the JDBC driver into a Maven project, create a Maven project and add the following dependencies to the pom.xml file:

      <dependency>
          <groupId>com.aliyun.lindorm</groupId>  
          <artifactId>lindorm-all-client</artifactId>
          <version>2.1.5</version>
      </dependency>
      Note

      Specify the version of lindorm-all-client based on your business requirements.

  3. Create a DataSource object, establish a JDBC connection based on the DataSource object, and then connect to LindormTSDB. The following code provides an example:

    import com.alibaba.druid.pool.DruidDataSource;
    import java.sql.*;
    
    public class App {
        private DruidDataSource dataSource;
    
        public void setUp() {
            dataSource = new DruidDataSource();
            try {
              // Specify the endpoint of LindormTSDB that you obtained from the Lindorm console.
                dataSource.setDriver(DriverManager.getDriver("jdbc:lindorm:tsdb:url=http://ld-bp17j28j2y7pm****-proxy-tsdb-pub.lindorm.rds.aliyuncs.com:8242"));
                dataSource.setUrl("jdbc:lindorm:tsdb:url=http://ld-bp17j28j2y7pm****-proxy-tsdb-pub.lindorm.rds.aliyuncs.com:8242");
            		dataSource.setValidationQuery("select 1");
            		dataSource.setMaxActive(1);
              // Specify a username or use the default username.
            		dataSource.setUsername("username");
              // Specify the password or use the default password.
            		dataSource.setPassword("****");
            		dataSource.setKeepAlive(true);
            		dataSource.setMinEvictableIdleTimeMillis(600000);
           			dataSource.setMaxWait(10000);
            		dataSource.init();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }     
        }
    
        public void close() {
            dataSource.close();
        }
    
        public void testQuery() {
            try (Connection conn = this.dataSource.getConnection()) {
                try (Statement stmt = conn.createStatement()) {
                    ResultSet rs = stmt.executeQuery("select * from sensor");
                    long count = 0;
                    while (rs.next()) {
                        count++;
                    }
                    System.out.println("count=" + count + " ");
                } catch (RuntimeException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            } catch (RuntimeException e) {
                System.out.println("connection failed.");
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public static void main( String[] args ) {
            App app = new App();
            app.setUp();
            app.testQuery();
            app.close();
        }
    }
    Note
    • For more information about the parameters that you can configure for the JDBC URL, see URL of the JDBC driver.

    • The default username and password are both root.