ManualPoolingDriverExample

代码
public static void setupDriver() throws Exception {
        ObjectPool connectionPool 
= new GenericObjectPool(null);
        Properties props 
= new Properties();
        props.setProperty(
"user""root");
        props.setProperty(
"password""123456");
        ConnectionFactory connectionFactory 
= new DriverManagerConnectionFactory("jdbc:mysql://localhost:3306/test", props);
        PoolableConnectionFactory poolableConnectionFactory 
= new PoolableConnectionFactory(connectionFactory, connectionPool, nullnullfalsetrue);
        Class.forName(
"org.apache.commons.dbcp.PoolingDriver");
        PoolingDriver driver 
= (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
        driver.registerPool(
"example", connectionPool);
    }
    
    
//Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example");

    
public static void printDriverStats() throws Exception {
        PoolingDriver driver 
= (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
        ObjectPool connectionPool 
= driver.getConnectionPool("example");

        System.out.println(
"NumActive: " + connectionPool.getNumActive());
        System.out.println(
"NumIdle: " + connectionPool.getNumIdle());
    }

    
public static void shutdownDriver() throws Exception {
        PoolingDriver driver 
= (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
        driver.closePool(
"example");
    }

 

posted @ 2010-06-20 13:36  Liren  阅读(286)  评论(0)    收藏  举报