proxool 连接池配置示例

本文介绍 proxool 连接池配置依赖、配置文件和示例代码。

配置依赖

<dependency>
    <groupId>com.oceanbase</groupId>
    <artifactId>oceanbase-client</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>proxool</groupId>
    <artifactId>proxool-cglib</artifactId>
    <version>0.9.1</version>
</dependency>
<dependency>
    <groupId>proxool</groupId>
    <artifactId>proxool</artifactId>
    <version>0.9.1_20141120</version>
</dependency>
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>

配置文件

jdbc-1.proxool.alias=test
jdbc-1.proxool.driver-class=com.oceanbase.jdbc.Driver
jdbc-1.proxool.driver-url=jdbc:oceanbase://xxx.xxx.xxx.xxx:1521/
jdbc-1.user=a****
jdbc-1.password=******

jdbc-1.proxool.maximum-connection-count=8
jdbc-1.proxool.minimum-connection-count=5
jdbc-1.proxool.prototype-count=4
jdbc-1.proxool.verbose=true
jdbc-1.proxool.statistics=10s,1m,1d
jdbc-1.proxool.statistics-log-level=error

示例代码

package com.springtest;

import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;
import org.testng.annotations.Test;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class ProXoolConnectPoolFactory {
    ProXoolConnectPoolFactory() throws FileNotFoundException {
        init();
    }
    public void init() throws FileNotFoundException {//把 properties 文件加载到链接对象
        String name = "proxool.properties";
        InputStream in = ProXoolConnectPoolFactory.class.getResourceAsStream(name);
        Properties properties = new Properties();
        try {
            properties.load(in);
            PropertyConfigurator.configure(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");// 加载数据库连接池配备的驱动
            conn = DriverManager.getConnection("proxool.test");//proxool 为配置文件名,test 为连接池别名
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    private void executeSqls(String[] sqls, Connection conn) {
        PreparedStatement ps;
        for (String sql : sqls) {
            try {
                ps = conn.prepareStatement(sql);
                ps.execute();
                ps.close();
            } catch (Exception e) {
                System.out.println("sql:" + sql);
                // ignore, maybe table does not exist
                e.printStackTrace();
            }
        }
    }
    @Test
    public void testInsert() throws SQLException {
        Connection connection = getConnection();
        System.out.println("autocommit is " + connection.getAutoCommit());
        try {
            //数据准备
            String[] sqls = {
                    "drop table t1",
                    "create table t1(c1 binary_double)"};
            executeSqls(sqls, connection);
        } catch (Exception exp) {
            exp.printStackTrace();
        }
        PreparedStatement ps = connection.prepareStatement("insert into t1 values('2.0')");
        ps.execute();
        ResultSet resultSet = ps.executeQuery("select * from t1");
        while (resultSet.next()) {
            int count  = resultSet.getMetaData().getColumnCount();
            for (int i = 1; i <= count; i++) {
                System.out.println(resultSet.getMetaData().getColumnName(i) + ":" + resultSet.getString(i));
            }
        }
    }
}