第一版数据库连接池

数据库连接池

package com.cn.thread.connectpool;

public interface IMyPool {
	PooledConnection getConnection();
	void createConnections(int count);
}
package com.cn.thread.connectpool;

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

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Driver;

public class MyPoolImpl implements IMyPool{
	private static String dirver = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/test";
	private static String user = "root";
	private static String password = "123456";
	
	private static int initCount = 3;
	private static int stepSize = 10;
	private static int poolMaxSize = 150;
	private static Vector<PooledConnection> pooledConnections = new Vector<PooledConnection>();
	
	public MyPoolImpl()
	{
		init();
	}
	
	private void init() {
		try {
			Driver dbDriver = (Driver)Class.forName(dirver).newInstance();
			DriverManager.registerDriver(dbDriver);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//createConnections(initCount);
	}
	
	@Override
	public synchronized PooledConnection getConnection() {
		// TODO Auto-generated method stub
		if(pooledConnections.size() == 0)
		{
			System.out.println("数据库连接池架构没有正常初始化,将启动补刀机制初始化");
			createConnections(initCount);
		}
		PooledConnection connection = getRealConnection();
		while(connection == null)
		{
			createConnections(stepSize);
			connection = getRealConnection();
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return connection;
	}
	
	private synchronized PooledConnection getRealConnection() {
		for (PooledConnection conn:pooledConnections) {
			if(!conn.isBusy())
			{
				Connection connection = conn.getConnection();
				
				try {
					if(!connection.isValid(2000))
					{
						connection = (Connection) DriverManager.getConnection(url,user,password);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				conn.setBusy(true);
				return conn;
			}
		}
		return null;
	}

	@Override
	public void createConnections(int count) {
		// TODO Auto-generated method stub
		if(poolMaxSize >0 &&(pooledConnections.size() + count) <= poolMaxSize)
		{
			for (int i = 0; i < count; i++) {
				try {
					Connection connection = (Connection) DriverManager.getConnection(url,user,password);
					PooledConnection pooledConnection = new PooledConnection(connection, false);
					pooledConnections.add(pooledConnection);
					System.out.println("初始化"+(i+1)+"管道");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}else{
			System.out.println("创建失败,创建参数非法");
		}
	}

}
package com.cn.thread.connectpool;

import java.sql.ResultSet;

public class MyPoolTest {

	
	private static MyPoolImpl poolImpl = PoolManager.getInstace();
	
	public  static void selectData()
	{
		PooledConnection connection = poolImpl.getConnection();
		ResultSet rs = connection.queryBysql("SELECT * FROM items");
		System.out.println("线程名称:"  + Thread.currentThread().getName());
		try {
			while(rs.next()){
				System.out.println(rs.getString("ID")+ "\t\t");
				System.out.println(rs.getString("name")+ "\t\t");
				System.out.println(rs.getString("age")+ "\t\t");
				System.out.println();
			}
			rs.close();
			connection.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args)
	{
		System.out.println("poolconnection");
		for(int i=0;i<1500;i++)
		{
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					selectData();
				}
			}).start();
		}
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

package com.cn.thread.connectpool;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;



public class PooledConnection {
	private boolean isBusy=false;
	
	private Connection connection;
	
	public boolean isBusy() {
		return isBusy;
	}
	public void setBusy(boolean isBusy) {
		this.isBusy = isBusy;
	}
	public Connection getConnection() {
		return connection;
	}
	public void setConnection(Connection connection) {
		this.connection = connection;
	}
	
	public PooledConnection(Connection connection,boolean isBusy) {
		this.connection = connection;
		this.isBusy = isBusy;
	}
	
	public void close()
	{
		this.isBusy = false;
	}
	
	public ResultSet queryBysql(String sql)
	{
		Statement sm = null;
		ResultSet rs = null;
		
		try {
			sm=(Statement) connection.createStatement();
			rs = sm.executeQuery(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	
}

package com.cn.thread.connectpool;

//静态内部类确保线程安全
public class PoolManager {

	private static class createPool{
		private static MyPoolImpl poolImpl = new MyPoolImpl();
	}
	
	public static MyPoolImpl getInstace() {
		return createPool.poolImpl;
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值