Proxool 连接池基本操作

大家都知道,现在假如要连接数据库,都要用到连接池

现在最优秀的,也是OpenSource的连接池,就数Proxool了,那我们就开始把

官网:

http://proxool.sourceforge.net/index.html

 

我自己写了一个连接池,很粗糙,但也能加深对连接池的理解:

package com.chenhailong;

import com.chenhailong.exception.DBException;
import com.mysql.jdbc.Connection;

public interface ConnectionPool {

    public Connection getConnection() throws DBException;

    public void releaseConnection(Connection conn);

}
 
package com.chenhailong;

import java.sql.DriverManager;
import java.util.Vector;

import com.chenhailong.exception.DBException;
import com.mysql.jdbc.Connection;

/**
 * 
 * @author Administrator
 *
 */
public class DBConnectionPool implements ConnectionPool {

    private static Vector<Connection> pool;
    private final int POOL_MAX_SIZE = 20;
    private static final String url = "jdbc:mysql://localhost:3306/test";
    private static final String user = "root";
    private static final String password = "****";

    @Override
    public synchronized Connection getConnection() throws DBException {
        if (pool == null) {
            pool = new Vector<Connection>();
        }
        Connection conn;
        if (pool.isEmpty()) {
            conn = createConnection();
        } else {
            int last_idx = pool.size() - 1;
            conn = (Connection) pool.get(last_idx);
            pool.remove(pool.get(last_idx));
        }
        return conn;
    }

    public static Connection createConnection() throws DBException {
        Connection conn = null;
        try {
            Class.forName("org.gjt.mm.mysql.Driver");
            conn = (Connection) DriverManager
                    .getConnection(url, user, password);
            System.out.println("Success to connect the mysql");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    @Override
    public synchronized void releaseConnection(Connection conn) {
        if (pool.size() > POOL_MAX_SIZE) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            pool.add(conn);
        }
    }

}
 
package com.chenhailong.exception;

public class DBException extends Exception {

    public DBException(){
        
    }
}

 

很简单,是把

 

我的当然比不上Proxool了

 

下面是关于Proxool的具体操作(如果有什么不明白的,请给我发站内信,我会常看)

 

/**
 * 
 */
package com.chenhailong;

import java.sql.Connection;


/**
 * @author Administrator
 *
 */
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Connection conn = PoolManager.getConnectionByPerpertiesUtil();
        PoolManager.executeQuery(conn);
        Connection connXml = PoolManager.getConnectionByXMLUtil();
        PoolManager.executeQuery(connXml);
        Connection connProperty = PoolManager.getConnectionByPropertyUtil();
        PoolManager.executeQuery(connProperty);
        PoolManager.close(connXml);
        PoolManager.close(conn);
        PoolManager.close(connProperty);
    }

}

 

package com.chenhailong;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.ProxoolFacade;
import org.logicalcobwebs.proxool.admin.SnapshotIF;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;

/**
 * 
 * @author chenhailong
 * 2011-8-31
 * 下午5:12:07
 */
public class PoolManager {

    private static int activeCount = 0;

    public PoolManager() {
    }

    public static Connection getConnectionByPerpertiesUtil() {
        Connection conn = null;
        try {
            Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
            Properties pro = new Properties();
            pro.setProperty("proxool.maximum-connection-count", "20");
            pro.setProperty("proxool.house-keeping-test-sql", "select CURRENT_DATE");
            pro.setProperty("user", "root");
            pro.setProperty("password", "***");
            String alias = "properties";
            String driverClass = "org.gjt.mm.mysql.Driver";
            String driverUrl = "jdbc:mysql://localhost:3306/test";
            String url = "proxool." + alias + ":" + driverClass + ":" + driverUrl;
            conn = DriverManager.getConnection(url, pro);
            printSnapShot(alias);
            return conn;
        } catch (Exception ex) {
            ex.printStackTrace();
        } 
        return null;
    }

    public static Connection getConnectionByXMLUtil() {
        Connection conn = null;
        try {
            Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
            JAXPConfigurator.configure("proxool.xml", false);
            conn = DriverManager.getConnection("proxool.xml");
            printSnapShot("xml");
            return conn;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static Connection getConnectionByPropertyUtil() {
        Connection conn = null;
        try {
            Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
            PropertyConfigurator.configure("proxool.properties");
            conn = DriverManager.getConnection("proxool.property");
            printSnapShot("property");
            return conn;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static void printSnapShot(String alias) throws ProxoolException {
        SnapshotIF snapshot = ProxoolFacade.getSnapshot(alias);
        int currentActiveCount = snapshot.getActiveConnectionCount();
        int availableCount = snapshot.getAvailableConnectionCount();
        int maxCount = snapshot.getMaximumConnectionCount();
        if (activeCount != currentActiveCount) {
            System.out.println("Active count :" + currentActiveCount);
            System.out.println("Available count :" + availableCount);
            System.out.println("Max count :" + maxCount);
        }
    }

    public static void close(Connection conn) {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void executeQuery(Connection conn) {
        Statement stat = null;
        ResultSet rs = null;
        try {
            // test so not use the preparedStatement
            stat = conn.createStatement();
            rs = stat.executeQuery("select * from test");
            while (rs.next()) {
                String id = rs.getString(1);
                String name = rs.getString(2);
                String age = rs.getString(3);
                String sex = rs.getString(4);
                String message = id + "\t" + name + "\t" + age + "\t" + sex;
                System.out.println(message);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                stat.close();
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

}
 
jdbc-0.proxool.alias=property
jdbc-0.proxool.driver-url=jdbc:mysql://localhost:3306/test
jdbc-0.proxool.driver-class=org.gjt.mm.mysql.Driver
jdbc-0.user=root
jdbc-0.password=***
jdbc-0.proxool.maximum-connection-count=20
jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE
 
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
	<proxool>
		<alias>xml</alias>
		<driver-url>jdbc:mysql://localhost:3306/test</driver-url>
		<driver-class>org.gjt.mm.mysql.Driver</driver-class>
		<driver-properties>
			<property name="user" value="root" />
			<property name="password" value="***" />
		</driver-properties>
		<maximum-connection-count>20</maximum-connection-count>
		<house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
	</proxool>
</something-else-entirely>

 

 

你可能感兴趣的:(proxool)