JDBC连接池_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > JDBC连接池

JDBC连接池

 2014/9/1 1:22:19  gegewuqin9  程序员俱乐部  我要评论(0)
  • 摘要:/****/packagebizcore.db;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.ArrayList;importjava.util.Properties;importorg.logicalcobwebs.proxool.ProxoolException;importorg
  • 标签:连接 连接池
class="java">

/**
 * 
 */
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教程之访问权限 下一篇: 没有下一篇了!
发表评论
用户名: 匿名