数据库连接池
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;
}
}