文档

C3P0 连接池配置示例

更新时间:

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

配置依赖

<dependency>
    <groupId>com.oceanbase</groupId>
    <artifactId>oceanbase-client</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5</version>
</dependency>

示例代码

public class C3P0PoolTest {
    private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
    public void init() throws PropertyVetoException {
        dataSource.setDriverClass("com.oceanbase.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:oceanbase://xxx.xxx.xxx.xxx:1521/?useSSL=false");
        dataSource.setUser("a****");
        dataSource.setPassword("******");
        dataSource.setInitialPoolSize(3);
        dataSource.setMaxPoolSize(10);
        dataSource.setMinPoolSize(3);
        dataSource.setAcquireIncrement(3);
    }
    public void testInsert() throws SQLException, PropertyVetoException {
        init();
        Connection connection =  dataSource.getConnection();
        System.out.println("autocommit is " + connection.getAutoCommit());
        connection.setAutoCommit(false);
        connection.prepareStatement("create table t1(c1 binary_double)").execute();
        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));
            }
        }
    }
}