博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自己测试的c3p0的代码demo
阅读量:5939 次
发布时间:2019-06-19

本文共 3706 字,大约阅读时间需要 12 分钟。

  hot3.png

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

你可能感兴趣的文章