自己测试的c3p0的代码demo

public class SimpleDataSourceDemo {
    public static ComboPooledDataSource cpds = new ComboPooledDataSource("dk-c3p0-pool");

    static {
        try {
            init();
        } catch (PropertyVetoException e) {
            e.printStackTrace();
            System.exit(0);
        }
    }

    private static void init() throws PropertyVetoException {
        cpds.setDriverClass(driverClass); //loads the jdbc driver
        cpds.setJdbcUrl( url );
        cpds.setUser(username);
        cpds.setPassword(pwd);
        cpds.setMaxStatements( 180 );

        // the settings below are optional -- c3p0 can work with defaults
        cpds.setMinPoolSize(5);
        cpds.setAcquireIncrement(5);
        cpds.setMaxPoolSize(20);
        cpds.setConnectionCustomizerClassName("db.pool.c3p0.MyConnectionCustomizer");
        cpds.setPreferredTestQuery("select  1");
        cpds.setTestConnectionOnCheckin(true);
        cpds.setTestConnectionOnCheckout(true);
        cpds.setIdentityToken("dk-identityToken");
    }

    public static Connection getConnection(){
        try {
            System.out.println("idleconn="+cpds.getNumIdleConnections());
            System.out.println("numconn="+cpds.getNumConnections());
            System.out.println("minPoolSize="+cpds.getMinPoolSize());
            System.out.println("maxPoolSize="+cpds.getMaxPoolSize());
            return cpds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws PropertyVetoException {
        long start = System.currentTimeMillis();

        try {
            Connection connection = SimpleDataSourceDemo.getConnection();
            long end = System.currentTimeMillis();
            System.out.println(end - start);


            ResultSet resultSet =  connection.prepareStatement("select * from testtb").executeQuery();
            while (resultSet.next()){
                System.out.println(resultSet.getInt("id")+","+resultSet.getString(2)+","+resultSet.getString(3));
            }
            connection.close();

            start = System.currentTimeMillis();
            connection = SimpleDataSourceDemo.getConnection();
            end = System.currentTimeMillis();
            System.out.println(end - start);

            start = System.currentTimeMillis();
            resultSet =  connection.prepareStatement("select * from testtb").executeQuery();
            end = System.currentTimeMillis();
            System.out.println(end - start);

            while (resultSet.next()){
                System.out.println(resultSet.getInt("id")+","+resultSet.getString(2)+","+resultSet.getString(3));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

 

 

 

public class MyConnectionCustomizer implements ConnectionCustomizer {
    public void onAcquire(Connection c, String parentDataSourceIdentityToken) throws Exception {
        System.out.println("acquire is invoke ================================="+parentDataSourceIdentityToken);
    }

    public void onDestroy(Connection c, String parentDataSourceIdentityToken) throws Exception {
        System.out.println("destory is invoke ================================="+parentDataSourceIdentityToken);
    }

    public void onCheckOut(Connection c, String parentDataSourceIdentityToken) throws Exception {
        System.out.println("checkout is invoke ================================="+parentDataSourceIdentityToken);
    }

    public void onCheckIn(Connection c, String parentDataSourceIdentityToken) throws Exception {
        System.out.println("checkIn is invoke ================================="+parentDataSourceIdentityToken);
    }
}

 

转载于:https://my.oschina.net/zhdkn/blog/691449

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值