package test.connectionpool;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;public class ConnectionPool
{ public ConnectionPool()
{
// TODO Auto-generated constructor stub
}
private int chackOut;
private List<Connection> pool = new ArrayList<Connection>();
private int maxCont;
private String username;
private String name;
private String password;
private String URL;
//name为连接池的名字
public ConnectionPool(String name, String URL, String username, String password, int maxcont) {
this.name = name;
this.URL = URL;
this.username = username;
this.password = password;
this.maxCont = maxcont;
}
public synchronized void freeConnection(Connection conn) {
pool.add(conn);
chackOut--;
notifyAll();
}
public synchronized Connection getConnection() {
Connection conn = null;
if(pool.size()>0) {
conn = pool.get(0);
pool.remove(0);
try {
if(conn.isClosed()) {
System.out.println("移除坏的链接" + name);
conn = getConnection();
}
} catch(Exception e) {
System.out.println("移除坏的链接" + name);
conn = getConnection();
}
} else if(maxCont ==0 || chackOut<maxCont) {
conn = newConnection();
if(conn !=null) {
this.chackOut++;
}
}
return conn;
}
public synchronized Connection getConnection(long timeout) {
long stattime = new Date().getTime();
Connection conn = null;
while((conn = getConnection()) == null) {
try {
wait(timeout);
} catch(Exception e) {
if(new Date().getTime()-stattime >= timeout) {
//超视
return null;
}
}
}
return conn;
}
//关闭所有链接
public synchronized void release() {
for(int i=0; i<pool.size(); i++) {
Connection conn = pool.get(i);
try
{
conn.close();
System.out.println("可以关闭");
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
//日志不能关闭
}
pool.remove(i);
}
}
private Connection newConnection() {
Connection conn = null;
try {
//xin
if(username == null) {
conn = DriverManager.getConnection(URL);
} else {
//用户名密码的链接
conn = DriverManager.getConnection("", "", "");
}
System.out.println("chuan jian xin lian jie");
} catch(Exception e) {
//异常
return null;
}
return conn;
}
}
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;public class ConnectionPool
{ public ConnectionPool()
{
// TODO Auto-generated constructor stub
}
private int chackOut;
private List<Connection> pool = new ArrayList<Connection>();
private int maxCont;
private String username;
private String name;
private String password;
private String URL;
//name为连接池的名字
public ConnectionPool(String name, String URL, String username, String password, int maxcont) {
this.name = name;
this.URL = URL;
this.username = username;
this.password = password;
this.maxCont = maxcont;
}
public synchronized void freeConnection(Connection conn) {
pool.add(conn);
chackOut--;
notifyAll();
}
public synchronized Connection getConnection() {
Connection conn = null;
if(pool.size()>0) {
conn = pool.get(0);
pool.remove(0);
try {
if(conn.isClosed()) {
System.out.println("移除坏的链接" + name);
conn = getConnection();
}
} catch(Exception e) {
System.out.println("移除坏的链接" + name);
conn = getConnection();
}
} else if(maxCont ==0 || chackOut<maxCont) {
conn = newConnection();
if(conn !=null) {
this.chackOut++;
}
}
return conn;
}
public synchronized Connection getConnection(long timeout) {
long stattime = new Date().getTime();
Connection conn = null;
while((conn = getConnection()) == null) {
try {
wait(timeout);
} catch(Exception e) {
if(new Date().getTime()-stattime >= timeout) {
//超视
return null;
}
}
}
return conn;
}
//关闭所有链接
public synchronized void release() {
for(int i=0; i<pool.size(); i++) {
Connection conn = pool.get(i);
try
{
conn.close();
System.out.println("可以关闭");
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
//日志不能关闭
}
pool.remove(i);
}
}
private Connection newConnection() {
Connection conn = null;
try {
//xin
if(username == null) {
conn = DriverManager.getConnection(URL);
} else {
//用户名密码的链接
conn = DriverManager.getConnection("", "", "");
}
System.out.println("chuan jian xin lian jie");
} catch(Exception e) {
//异常
return null;
}
return conn;
}
}
{
init();
}
private static DBConnectionManager instence;
//统计连接数用
private static int contents;
private List<Driver> list = new ArrayList<Driver>();
private PrintWriter log;
private Map<String, ConnectionPool> pools = new HashMap<String, ConnectionPool>();
if(instence == null) {
instence = new DBConnectionManager();
}
contents++;
return instence;
} //归还链接
public void freeConnection(String name, Connection conn) {
ConnectionPool pool = pools.get(name);
if(pool!=null) {
pool.freeConnection(conn);
}
}