To: [EMAIL PROTECTED]
Subject: Re: JDBC pooling
From: James Cook <[EMAIL PROTECTED]>
Date: Wed, 17 Mar 1999 19:12:35 -0500
Importance: Normal
In-Reply-To: <[EMAIL PROTECTED]>
Reply-To: A mailing list for Enterprise JavaBeans development
<[EMAIL PROTECTED]>
Sender: A mailing list for Enterprise JavaBeans development
<[EMAIL PROTECTED]>
----------------------------------------------------------------------------
----
Jim,
Here is some code that I have written to allow database connection pooling.
No docs yet, but pretty simple interface...
package com.vxs.database;
import java.sql.*;
import java.util.*;
public class DBPool {
public static final int poolAvailable = 0;
public static final int poolActive = 1;
private Vector pool = null;
private String dbaseURL = null;
private String username = null;
private String password = null;
private int maxConn;
public DBPool(String dbaseURL) {
this(dbaseURL, null, null);
}
public DBPool (String dbaseURL, String username, String password) {
this.dbaseURL = dbaseURL;
this.username = username;
this.password = password;
setMaxConn (3);
pool = new Vector(maxConn);
// DriverManager.setLogStream(System.out);
}
public synchronized Connection getConnection () throws SQLException {
Connection conn = null;
while ((conn = tryGetConnection()) == null) {
try {
wait();
} catch (InterruptedException xie) {}
}
conn.setAutoCommit(true);
return conn;
}
public synchronized Connection tryGetConnection () throws SQLException {
PoolConnection pConn = null;
Enumeration e = pool.elements();
while (e.hasMoreElements()) {
PoolConnection nextConn = (PoolConnection)e.nextElement();
if (nextConn.getStatus() == poolAvailable) {
pConn = nextConn;
pConn.setStatus (poolActive);
break;
}
}
if (pConn == null && pool.size() < maxConn) {
if (username == null)
pConn = new PoolConnection (DriverManager.getConnection
(dbaseURL));
else
pConn = new PoolConnection (DriverManager.getConnection
(dbaseURL, username, password));
pConn.setStatus (poolActive);
pool.addElement (pConn);
} else {
// Connections are all active and can't create
}
if (pConn == null)
return null;
else
return pConn.getConnection();
}
public synchronized void releaseConnection (Connection pConn) {
if (pConn == null) return;
Enumeration e = pool.elements();
while (e.hasMoreElements()) {
PoolConnection nextConn = (PoolConnection)e.nextElement();
if (nextConn.getConnection() == pConn) {
nextConn.setStatus (poolAvailable);
notify();
break;
}
}
}
public int getMaxConn() {
return maxConn;
}
public void setMaxConn(int newMaxConn) {
maxConn = newMaxConn;
}
public static String sqlEncode(String sql) {
if (sql == null) return "";
StringBuffer sb = new StringBuffer(sql);
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == '\'') {
sb.insert(i, '\\');
i++;
} else
if (sb.charAt(i) == '\\') {
sb.insert(i, '\\');
i++;
}
}
return sb.toString();
}
}
class PoolConnection {
private int status;
private java.sql.Connection connection;
public PoolConnection (Connection connection) {
this.status = DBPool.poolAvailable;
this.connection = connection;
}
public int getStatus() {
return status;
}
public void setStatus(int newStatus) {
status = newStatus;
}
public void setConnection(java.sql.Connection newConnection) {
connection = newConnection;
}
public java.sql.Connection getConnection() {
return connection;
}
}
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets