几种Java数据库连接池实现(三)<2>

ackage pool;

//连接池调度线程
public class FactoryMangeThread implements Runnable {
ConnectionFactory cf = null;

long delay = 1000;

public FactoryMangeThread(ConnectionFactory obj) {
cf = obj;
}

/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*
*/
public void run() {
while (true) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
}
System.out.println("eeeee");
// 判断是否已经关闭了工厂,那就退出监听
if (cf.isCreate())
cf.schedule();
else
System.exit(1);
}
}
}


package pool;

//连接池工厂参数
public class FactoryParam {
// 最大连接数
private int MaxConnectionCount = 4;

// 最小连接数
private int MinConnectionCount = 2;

// 回收策略
private int ManageType = 0;

public FactoryParam() {
}

/**
*
* 构造连接池工厂参数的对象
*
* @param max
* 最大连接数
*
* @param min
* 最小连接数
*
* @param type
* 管理策略
*
*/
public FactoryParam(int max, int min, int type) {
this.ManageType = type;
this.MaxConnectionCount = max;
this.MinConnectionCount = min;
}

/**
*
* 设置最大的连接数
*
* @param value
*
*/
public void setMaxConn(int value) {
this.MaxConnectionCount = value;
}

/**
*
* 获取最大连接数
*
* @return
*
*/
public int getMaxConn() {
return this.MaxConnectionCount;
}

/**
*
* 设置最小连接数
*
* @param value
*
*/
public void setMinConn(int value) {
this.MinConnectionCount = value;
}

/**
*
* 获取最小连接数
*
* @return
*
*/
public int getMinConn() {
return this.MinConnectionCount;
}

public int getType() {
return this.ManageType;
}
}


===
package pool;

import java.sql.*;

public class testmypool {
public void test1() {
String user = "DevTeam";
String password = "DevTeam";
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:gfqh2";
ConnectionParam param = new ConnectionParam(driver, url, user, password);
ConnectionFactory cf = null;// new ConnectionFactory(param, new
// FactoryParam());
try {
cf = new ConnectionFactory(param, new FactoryParam());
Connection conn1 = cf.getFreeConnection();
Connection conn2 = cf.getFreeConnection();
Connection conn3 = cf.getFreeConnection();
Statement stmt = conn1.createStatement();
ResultSet rs = stmt.executeQuery("select * from requests");
if (rs.next()) {
System.out.println("conn1 y");
} else {
System.out.println("conn1 n");
}
stmt.close();
conn1.close();
Connection conn4 = cf.getFreeConnection();
Connection conn5 = cf.getFreeConnection();
stmt = conn5.createStatement();
rs = stmt.executeQuery("select * from requests");
if (rs.next()) {
System.out.println("conn5 y");
} else {
System.out.println("conn5 n");
}
conn2.close();
conn3.close();
conn4.close();
conn5.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
cf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
String user = "DevTeam";
String password = "DevTeam";
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:gfqh2";
ConnectionParam param = new ConnectionParam(driver, url, user, password);
ConnectionFactory cf = null;
// new ConnectionFactory(param,new FactoryParam());
try {
cf = new ConnectionFactory(param, new FactoryParam());
// ConnectionFactory cf1 = new ConnectionFactory(param,new
// FactoryParam());
Connection conn1 = null;
long time = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
conn1 = cf.getFreeConnection();
Statement stmt = conn1.createStatement();
ResultSet rs = stmt.executeQuery("select * from requests");
if (rs.next()) {
System.out.println("conn1 y");
} else {
System.out.println("conn1 n");
}
conn1.close();
}
System.out.println("pool:" + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
Class.forName(param.getDriver()).newInstance();
for (int i = 0; i < 10; i++) {
conn1 = DriverManager.getConnection(param.getUrl(), param
.getUser(), param.getPassword());
Statement stmt = conn1.createStatement();
ResultSet rs = stmt.executeQuery("select * from requests");
if (rs.next()) {
System.out.println("conn1 y");
} else {
System.out.println("conn1 n");
}
conn1.close();
}
System.out
.println("no pool:" + (System.currentTimeMillis() - time));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
cf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值