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;
    }

}

> -----Original Message-----
> From: A mailing list for Enterprise JavaBeans development
> [mailto:[EMAIL PROTECTED]]On Behalf Of Jim Frentress
> Sent: Monday, March 15, 1999 4:33 PM
> To: [EMAIL PROTECTED]
> Subject: JDBC pooling
>
>
> if you will, please point me to the location of a jdbc pooling mechanism
> (preferrably including source and in the public domain). these things are
> like clocks, when you don't need one there are dozens around. when you do,
> you can't remember where the heck they are.
>
> ==================================================================
> =========
> To unsubscribe, send email to [EMAIL PROTECTED] and include
> in the body
> of the message "signoff EJB-INTEREST".  For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
>
>

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to