读书人

数据库连接池,该怎么处理

发布时间: 2012-01-13 22:43:29 作者: rapoo

数据库连接池

下面是ConnectionObject类主要方法的程序代码:
package guo.test.ConnectionPool;
public class ConnectionObject {
private Connection conn; // 数据库连接属性;
private boolean isUse; // 连接使用状态属性,连接对象当前是否在使用;
private int useCount; // 连接使用次数属性,记录连接对象的使用次数
public ConnectionObject(String url, String username, String password) {
try {
if (username.equals( " ") || password.equals( " ")) {
conn = DriverManager.getConnection(url);
} else {
conn = DriverManager.getConnection(url, username, password);
}
} catch (SQLException e) {
System.err.print(e.getMessage());
}
catch(NullPointerException e){
System.err.print(e.getMessage());
}
isUse = false;// 初始化连接对象的使用状态为空闭
useCount = 0;// 初始化连接对象的使用次数为0
}
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
public boolean getIsUse() {
return isUse;
}
public void setIsUse(boolean isUse) {
this.isUse = isUse;
}
public int getUseCount() {
return useCount;
}
public void setUseCount(int useCount) {
this.useCount = useCount;
}
public int addUseCount() {
return useCount++;
}
}

package guo.test.ConnectionPool;

public class ConnectionPool {
private String driver = " "; // 数据库驱动程序;
private String url = " "; // 数据库存放的url;
private String username = " "; // 数据库用户名;
private String password = " "; // 数据库用户密码;
private static Vector pool = new Vector(); // 用Vector对象作为连接池;
private int connectionPoolSize; // 初始化时,连接池中建立的连接对象数目
private int connectionPoolMaxsize; // 连接池的上限,至多能建立的连接连接对象数目;
private int connectionmaxUseCount; // 一个连接对象最多能被使用的次数;
private int timeout; // 用来记录连接超时的时间;
private void loadprop() {
InputStream is = getClass().getResourceAsStream( "dbconfig.txt ");
Properties props = new Properties();
try {
props.load(is);
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
driver = props.getProperty( "db.driver ");
url = props.getProperty( "db.url ");
username = props.getProperty( "db.username ");
password = props.getProperty( "db.password ");
connectionPoolSize = Integer.parseInt(props
.getProperty( "db.minconnect "));
connectionPoolMaxsize = Integer.parseInt(props
.getProperty( "db.maxconnection "));


connectionmaxUseCount = Integer.parseInt(props
.getProperty( "db.connectionmaxusecount "));
timeout = Integer.parseInt(props.getProperty( "timeout "));
}

public ConnectionPool() {
System.out.println( "调用loadprop() ");
loadprop();
}

private void initPool() {
System.out.println( "初始化连接池 ");
try {
Class.forName(driver);
System.out.println( "驱动程序是: " + driver);
} catch (ClassNotFoundException e) {
System.err.print(e.getMessage());
}
// 创建connectionPoolSize个空闭的连接对象,并把它们加入连接池中
while (pool.size() < connectionPoolSize) {
ConnectionObject newConn = new ConnectionObject(url, username,password);
pool.addElement(newConn);// 将连接对象加入连接池中
if (newConn != null) {
System.out.println( "你建立了第 " + (pool.indexOf(newConn) + 1)
+ "个连接 ");
} else {
System.out.println( "连接有问题,请检查! ");
}
}
for (int i = 0; i < pool.size(); i++) {
System.out.println(pool.get(i).toString());
}
}

private synchronized ConnectionObject getFreeConnection() {
ConnectionObject conn = null;
Iterator iter = pool.iterator();
while (iter.hasNext()) {
conn = (ConnectionObject) iter.next();
if (conn.getUseCount() == 0 && conn.getIsUse() == false) {
// conn.setIsUse(true);
// conn.addUseCount();
return conn;
}
if(!conn.getIsUse()&&conn.getUseCount() <connectionmaxUseCount) {
System.out.println( "没有空闲的连接,看是否有可复用的! ");
return conn;
}
}
return null;
}
public synchronized Connection getConnection() {
ConnectionObject connectionobj = getFreeConnection();
if (connectionobj == null && pool.size() < connectionPoolMaxsize) {
System.out.println( "还没有达到连接池的上限,可以创建连接! ");
ConnectionObject newConn = new ConnectionObject(url, username,password);
pool.addElement(newConn);
System.out.println( "建立了第 " + (pool.indexOf(newConn) + 1) + "个连接 ");
connectionobj = getFreeConnection();
}
if (connectionobj != null) {
connectionobj.setIsUse(true);
connectionobj.addUseCount();
return connectionobj.getConn();
} else {
try {
Thread.sleep(timeout);
} catch (Exception e) {
System.err.print(e.getMessage());
}
}
connectionobj = getFreeConnection();
if (connectionobj != null) {
connectionobj.setIsUse(true);
connectionobj.addUseCount();
return connectionobj.getConn();
}
return null;
}
// 找到连接的ID,某前连接的是那一个连接,这个方法中的if语句就没有执行,但是pool检测却不为空的。请那位高手指教。
private int findConnectionID(Connection conn) {
int connectionID = -1;


if (pool.contains(conn)) {
connectionID = pool.indexOf(conn);
return connectionID;
}
return connectionID;
}

public synchronized void release(Connection conn) {
int index = findConnectionID(conn);
if (index == -1) {
return;
}
ConnectionObject co = (ConnectionObject) pool.elementAt(index);
if (co.getUseCount() > = connectionmaxUseCount) {
pool.remove(index);
} else {
System.out.println( "放回了连接池 ");
co.setIsUse(false);// 置连接对象的使用状态为空闲
}
}

public synchronized void finalize() {
for (int i = 0; i < pool.size(); i++) {
ConnectionObject conn = (ConnectionObject) pool.elementAt(i);
try {
conn.getConn().close();
}
// 关闭连接对象
catch (SQLException e) {
System.err.print(e.getMessage());
}
conn = null; // 释放连接对象
}
// 清空连接池
pool = null;
if (pool == null) {
System.out.println( "连接池的连接全部被清空! ");
}
}

public static void main(String[] args) {
ConnectionPool cp = new ConnectionPool();
cp.initPool();
String sql = "select * from students ";
Statement stmt = null;
ResultSet rs = null;
Connection[] conns = new Connection[5];
for (int i = 0; i < conns.length; i++) {
conns[i] = cp.getConnection();
if (conns[i] != null) {
System.out.println( "可以使用连接了! ");
}
//int j = cp.findConnectionID(conns[i]);
//ystem.out.println( "你目前使用的是 " + (j + 1) + "号连接 ");
try {
stmt = conns[i].createStatement();
rs = stmt.executeQuery(sql);
if (rs != null) {
ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();
for (int k = 1; k <= numCols; k++) {
if (k > 1)
System.out.print( "..... ");
System.out.print(rsmd.getColumnName(k));
}
System.out.println( " ");
while (rs.next()) {
System.out.print(rs.getString(1) + "\t ");
System.out.print(rs.getString(2) + "\t ");
System.out.print(rs.getString(3) + "\t ");
System.out.print(rs.getString(4) + "\t ");
System.out.print(rs.getString(5) + "\t ");
System.out.print(rs.getString(6) + "\t ");
System.out.print(rs.getString(7) + "\t ");
System.out.println();
}
}
System.out.println( ".............................. "); System.out.println( ".............................. ");


//cp.release(conns[i]);
}
catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
cp.finalize();
}
}


[解决办法]
请问楼主想干嘛
[解决办法]
一般应用服务器都有连接池。不必自己来做。

读书人网 >J2SE开发

热点推荐