All Products
Search
Document Center

ApsaraDB for OceanBase:Example of configuring a HiKariCP connection pool

Last Updated:Aug 01, 2023

This topic describes how to configure dependencies and configuration files for a HiKariCP connection pool and provides relevant sample code.

Configure dependencies

pom.xml

<dependency>
    <groupId>com.oceanbase</groupId>
    <artifactId>oceanbase-client</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>3.3.1</version>
</dependency>

Configuration file

jdbc.properties

jdbcUrl=jdbc:oceanbase://xxx.xxx.xxx.xxx:1521/?useSSL=false&useServerPrepStmts=true&serverTimezone=UTC
username=a****
password=******
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
dataSource.useServerPrepStmts=true
dataSource.useLocalSessionState=true
dataSource.rewriteBatchedStatements=true
dataSource.cacheResultSetMetadata=true
dataSource.cacheServerConfiguration=true
dataSource.elideSetAutoCommits=true
dataSource.maintainTimeStats=false

Sample code

@Test
public void hikariTest() throws SQLException {
    HikariConfig config = new HikariConfig("/jdbc.properties");
    Connection conn = new HikariDataSource(config).getConnection();
    PreparedStatement drop_table_data_test = conn.prepareStatement("drop table data_test");
    Statement stmt = conn.createStatement();
    try {
        drop_table_data_test.execute();
    } catch (SQLException e) {
    }
    conn.prepareStatement("create table data_test(test1 int)").execute();
        for (int i = 0; i <= 10; i++) {
            stmt.executeUpdate("insert into data_test values("+i+")");
        }
    ResultSet rs = conn.prepareStatement("select * from data_test;").executeQuery();
    while (rs.next()){
        System.out.print(rs.getObject(1)+"\t");
    }
    System.out.println();
    System.out.println("------------------------");
    stmt.executeUpdate("update data_test set test1 = 100;");
    ResultSet rs1 = conn.prepareStatement("select * from data_test;").executeQuery();
    while (rs1.next()){
        System.out.print(rs1.getObject(1)+"\t");
    }
    System.out.println();
    System.out.println("------------------------");
    stmt.executeUpdate("delete from data_test");
    ResultSet rs2 = stmt.executeQuery("select * from data_test;");
    if (!rs2.next()){
        System.out.println("Data deleted");
    }
    stmt.close();
    drop_table_data_test.execute();
    drop_table_data_test.close();
    rs.close();
    conn.close();
}