自己实现数据库连接池

package myd.utils;

import java.util.*;
import java.sql.*;


import org.junit.Test;

/**
 * 2012-12-28
 * @author fs
 * DBConnectionPool
 */
public class DBConnectionPool {
	  
	
	
	public DBConnectionPool(int poolsize)
	{
		this.poolsize = poolsize;
		init();
	}
	
	
	public DBConnectionPool()
	{
		this(1);
	}
	
	private int poolsize = 0;
	
	
	private HashMap<Connection,String> dbconncoolmap = new HashMap<Connection,String>();
	//private List<HashMap<Connection,String>> pollconnList = new ArrayList<HashMap<Connection,String>>();
	String drivername = "oracle.jdbc.driver.OracleDriver";
	String driverURL="jdbc:oracle:thin:@192.168.90.187:1521:dev";
	String username="jztmsg";
	String password = "123";
	
	private final String  STATCE= "ISABLE";
	private final String  NOTSTATCE= "ISNOEABLE";
	
	public void init()
	{
		for(int i = poolsize;i>0;i--)
		{	
			dbconncoolmap.put(this.getConnection(), STATCE);
		}
	}
	
	private Connection getConnection()
	{
		Connection conn = null;
		try {
			Class.forName(drivername);
			conn = DriverManager.getConnection(driverURL, username, password);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return conn;
	}
	
	
	public Connection getConnectionformPool()
	{
		Connection conn = null;
		boolean isconnectionflag = true;
		for(Map.Entry<Connection, String> myentry : dbconncoolmap.entrySet())
		{
			synchronized (myentry) {
					 	if(myentry.getValue().equals(STATCE))
						{					 		
					 		myentry.setValue(NOTSTATCE);
							return myentry.getKey();
						}
					 	isconnectionflag = false;
					 }
		}
		
		if(!isconnectionflag)
		{
			conn = this.getConnection();
			dbconncoolmap.put(conn, NOTSTATCE);
		}
		return conn;
	}
	
	
	
	public void closeConnection(Connection conn)
	{
		for(Map.Entry<Connection, String> myentry : dbconncoolmap.entrySet())
		{
			synchronized (myentry) {
				 	if(myentry.getKey().equals(conn))
					{
				 		myentry.setValue(STATCE);
					}
			}
		}
	}
	
	public int getIsablePoolsize()
	{
		int i= 0;
		for(Map.Entry<Connection, String> myentry : dbconncoolmap.entrySet())
		{
				 	if(myentry.getValue().equals(STATCE))
					{
				 		i++;
					}
		}
		return i;
	}
	
	public int getPoolsize()
	{
		
		return this.dbconncoolmap.size();
	}
	
	
	
	@Test
	public void one()
	{
		String sql = "select * from msg_company";
		DBConnectionPool dbconnectionpool = new DBConnectionPool(3);
		Connection conn = dbconnectionpool.getConnectionformPool();
		System.out.println("getIsablePoolsize:"+dbconnectionpool.getIsablePoolsize()+"poolsize:"+dbconnectionpool.getPoolsize());
		try {
			Statement state = conn.createStatement();
			ResultSet resultset = state.executeQuery(sql);	
			while (resultset.next()){ 
				System.out.println(resultset.getLong(1));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbconnectionpool.closeConnection(conn);
		}
		Connection conn2 = dbconnectionpool.getConnectionformPool();
		System.out.println("getIsablePoolsize:"+dbconnectionpool.getIsablePoolsize()+"poolsize:"+dbconnectionpool.getPoolsize());
		Connection conn3 = dbconnectionpool.getConnectionformPool();
		System.out.println("getIsablePoolsize:"+dbconnectionpool.getIsablePoolsize()+"poolsize:"+dbconnectionpool.getPoolsize());
		Connection conn4 = dbconnectionpool.getConnectionformPool();
		System.out.println("getIsablePoolsize:"+dbconnectionpool.getIsablePoolsize()+"poolsize:"+dbconnectionpool.getPoolsize());
		Connection conn5 = dbconnectionpool.getConnectionformPool();
		System.out.println("getIsablePoolsize:"+dbconnectionpool.getIsablePoolsize()+"poolsize:"+dbconnectionpool.getPoolsize());
		
		
	}
	
	
	public static void main(String[] args) {
		
	}


	public String getDrivername() {
		return drivername;
	}


	public void setDrivername(String drivername) {
		this.drivername = drivername;
	}


	public String getDriverURL() {
		return driverURL;
	}


	public void setDriverURL(String driverURL) {
		this.driverURL = driverURL;
	}


	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public void setPoolsize(int poolsize) {
		this.poolsize = poolsize;
	}
	
				
}

你可能感兴趣的:(数据库连接池)