JDBC连接池


/**
 * 
 */
package bizcore.db;

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

import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.ProxoolFacade;

import bizcore.core.utils.Utils;

/**
 * @author ljf
 *
 */
public class ConnectionDBPool {

	private static ConnectionDBPool pool=null;
	private static Properties info = new Properties();
	private static String url="";
	private final static int databaseConnectionNum = 15; 
    private final static int maxConnectNum = 100; 
    private static int checkOut = 0;
    private static ArrayList<Connection> connectlist = new ArrayList<Connection>(); 
    private static ArrayList<Statement>  stmtList = new ArrayList<Statement>(); 
	
	private final static String alias = "pool"; 
	private final static String driverClass = "com.mysql.jdbc.Driver"; 
	private final static String driverUrl = "jdbc:mysql://localhost:3306/testdb?useUnicode=true&amp;characterEncoding=UTF-8"; 
	
    public static void createDBConnectionPool(){ 
        for (int i = connectlist.size(); i < databaseConnectionNum; i++) { 
        	pool = new ConnectionDBPool(); 
        	Connection connobject=null; 
        	connobject = pool.getConnection(); 
        	connectlist.add(connobject); 
        } 
    } 
	private ConnectionDBPool(){
		try {
			Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); 
			info.setProperty("proxool.maximum-connection-count", "20"); 
			info.setProperty("proxool.house-keeping-test-sql", "select CURRENT_DATE"); 
			info.setProperty("user", "root"); 
			info.setProperty("password", "root"); 
			
			url = "proxool." + alias + ":" + driverClass + ":" + driverUrl; 
			try { 
				ProxoolFacade.registerConnectionPool(url, info); 
			} catch (ProxoolException ex) { 
				ex.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	public static Connection getConnection(){
		Connection connection = null;
		try {
			connection = DriverManager.getConnection(url, info);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
	public synchronized static ConnectionDBPool getInstance(){
		if(Utils.isNull(pool)){
			pool = new ConnectionDBPool();
		}
		return pool;
	}
}

你可能感兴趣的:(java,jdbc)