Javadoc first draft.

This commit is contained in:
Erik C. Thauvin 2004-03-17 07:58:33 +00:00
parent bfc101df5b
commit 062c6b8fe6
9 changed files with 482 additions and 246 deletions

View file

@ -5,3 +5,4 @@ bin
dist dist
build build
classes classes
javadoc

View file

@ -4,4 +4,9 @@ path.classes=build
path.src=src path.src=src
path.dist=dist path.dist=dist
path.lib=lib path.lib=lib
path.javadoc=javadoc
javadoc.title=SimplePool
javadoc.api.link=http://java.sun.com/j2se/1.4.2/docs/api
javadoc.servlet.link=http://java.sun.com/products/servlet/2.3/javadoc/
javadoc.packages=net.java.dev.simplepool.*

View file

@ -13,6 +13,7 @@
<tstamp /> <tstamp />
<mkdir dir="${path.classes}"/> <mkdir dir="${path.classes}"/>
<mkdir dir="${path.dist}"/> <mkdir dir="${path.dist}"/>
<mkdir dir="${path.javadoc}"/>
</target> </target>
<target name="compile" depends="init" description="Compiles sources"> <target name="compile" depends="init" description="Compiles sources">
<mkdir dir="${path.classes}" /> <mkdir dir="${path.classes}" />
@ -27,10 +28,21 @@
</copy> </copy>
<jar destfile="${basedir}/${path.dist}/${appname}.jar" basedir="${path.classes}" excludes="**/.dependency-info/**" /> <jar destfile="${basedir}/${path.dist}/${appname}.jar" basedir="${path.classes}" excludes="**/.dependency-info/**" />
</target> </target>
<target name="javadoc" depends="init" description="Builds the Javadoc">
<delete quiet="true" includeEmptyDirs="true">
<fileset dir="${path.javadoc}" includes="*,*/**"/>
</delete>
<javadoc sourcepath="${path.src}" destdir="${path.javadoc}" packagenames="${javadoc.packages}" windowtitle="${javadoc.title}" verbose="false" failonerror="true" additionalparam="">
<link href="${javadoc.api.link}"/>
<link href="${javadoc.servlet.link}"/>
<classpath refid="path.class"/>
</javadoc>
</target>
<target name="clean" depends="init" description="Removes classses and javadoc"> <target name="clean" depends="init" description="Removes classses and javadoc">
<delete quiet="true" includeEmptyDirs="true"> <delete quiet="true" includeEmptyDirs="true">
<fileset dir="${path.classes}" includes="*,*/**"/> <fileset dir="${path.classes}" includes="*,*/**"/>
<fileset dir="${path.dist}" includes="*,*/**"/> <fileset dir="${path.dist}" includes="*,*/**"/>
<fileset dir="${path.javadoc}" includes="*,*/**"/>
</delete> </delete>
</target> </target>
</project> </project>

View file

@ -28,11 +28,11 @@ import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
/** /**
* SimplePool * Creates and manages a pool of database connections.
* *
* @author Marc A. Mnich * @author <a href="http://www.javaexchange.com/">Marc A. Mnich</a>
* @author Russell Beattie * @author <a href="http://www.russellbeattie.com/">Russell Beattie</a>
* @author Erik C. Thauvin * @author <a href="http://www.thauvin.net/erik/">Erik C. Thauvin</a>
* @version $Revision$, $Date$ * @version $Revision$, $Date$
* @since 1.0 * @since 1.0
*/ */
@ -64,18 +64,20 @@ public class SimplePool implements Runnable {
/** /**
* Creates a new Connection Broker<br> * Creates a new Connection Broker
* driver: JDBC driver. e.g. 'oracle.jdbc.driver.OracleDriver'<br> *
* jdbcUrl: JDBC connect string. e.g. 'jdbc:oracle:thin:@203.92.21.109:1526:orcl'<br> * @param driver JDBC driver. e.g. 'oracle.jdbc.driver.OracleDriver'
* user: Database login name. e.g. 'Scott'<br> * @param jdbcUrl JDBC connect string. e.g. 'jdbc:oracle:thin:@203.92.21.109:1526:orcl'
* password: Database password. e.g. 'Tiger'<br> * @param user Database login name. e.g. 'Scott'
* minConns: Minimum number of connections to start with.<br> * @param password Database password. e.g. 'Tiger'
* maxConns: Maximum number of connections in dynamic pool.<br> * @param minConns Minimum number of connections to start with.
* maxConnTime: Time in days between connection resets. (Reset does a basic cleanup)<br> * @param maxConns Maximum number of connections in dynamic pool.
* maxCheckoutSeconds: Max time a connection can be checked out before being recycled. Zero value turns option off, default is 60 seconds. * @param maxConnTime Time in days between connection resets. (Reset does a basic cleanup)
* @param maxCheckoutSeconds Max time a connection can be checked out before being recycled. Zero value turns option
* off, default is 60 seconds.
*/ */
public SimplePool(String driver, String jdbcUrl, String user, public SimplePool(String driver, String jdbcUrl, String user,
String password, int minConns, int maxConns, double maxConnTime, int maxCheckoutSeconds) String password, int minConns, int maxConns, double maxConnTime, int maxCheckoutSeconds)
throws IOException { throws IOException {
this.connPool = new Connection[maxConns]; this.connPool = new Connection[maxConns];
@ -109,9 +111,9 @@ public class SimplePool implements Runnable {
// Initialize the pool of connections with the mininum connections: // Initialize the pool of connections with the mininum connections:
// Problems creating connections may be caused during reboot when the // Problems creating connections may be caused during reboot when the
// servlet is started before the database is ready. Handle this // servlet is started before the database is ready. Handle this
// by waiting and trying again. The loop allows 5 minutes for // by waiting and trying again. The loop allows 5 minutes for
// db reboot. // db reboot.
boolean connectionsSucceeded = false; boolean connectionsSucceeded = false;
int dbLoop = 20; int dbLoop = 20;
@ -154,12 +156,12 @@ public class SimplePool implements Runnable {
/** /**
* Housekeeping thread. Runs in the background with low CPU overhead. * Housekeeping thread. Runs in the background with low CPU overhead. Connections are checked for warnings and
* Connections are checked for warnings and closure and are periodically * closure and are periodically restarted.
* restarted. * <p/>
* This thread is a catchall for corrupted * This thread is a catchall for corrupted connections and prevents the buildup of open cursors. (Open cursors
* connections and prevents the buildup of open cursors. (Open cursors
* result when the application fails to close a Statement). * result when the application fails to close a Statement).
* <p/>
* This method acts as fault tolerance for bad connection/statement programming. * This method acts as fault tolerance for bad connection/statement programming.
*/ */
public void run() { public void run() {
@ -271,17 +273,13 @@ public class SimplePool implements Runnable {
} // End run } // End run
/** /**
* This method hands out the connections in round-robin order. * This method hands out the connections in round-robin order. This prevents a faulty connection from locking up an
* This prevents a faulty connection from locking * application entirely. A browser 'refresh' will get the next connection while the faulty connection is cleaned up
* up an application entirely. A browser 'refresh' will * by the housekeeping thread.
* get the next connection while the faulty
* connection is cleaned up by the housekeeping thread.
* <p/> * <p/>
* If the min number of threads are ever exhausted, new * If the min number of threads are ever exhausted, new threads are added up the the max thread count. Finally, if
* threads are added up the the max thread count. * all threads are in use, this method waits 2 seconds and tries again, up to ten times. After that, it returns a
* Finally, if all threads are in use, this method waits * null.
* 2 seconds and tries again, up to ten times. After that, it
* returns a null.
*/ */
public Connection getConnection() { public Connection getConnection() {
@ -385,8 +383,7 @@ public class SimplePool implements Runnable {
} }
/** /**
* Frees a connection. Replaces connection back into the main pool for * Frees a connection. Replaces connection back into the main pool for reuse.
* reuse.
*/ */
public String freeConnection(Connection conn) { public String freeConnection(Connection conn) {
String res = ""; String res = "";
@ -405,8 +402,7 @@ public class SimplePool implements Runnable {
} }
/** /**
* Returns the age of a connection -- the time since it was handed out to * Returns the age of a connection -- the time since it was handed out to an application.
* an application.
*/ */
public long getAge(Connection conn) { // Returns the age of the connection in millisec. public long getAge(Connection conn) { // Returns the age of the connection in millisec.
int thisconn = idOfConnection(conn); int thisconn = idOfConnection(conn);
@ -437,29 +433,27 @@ public class SimplePool implements Runnable {
} }
/** /**
* Shuts down the housekeeping thread and closes all connections * Shuts down the housekeeping thread and closes all connections in the pool. Call this method from the destroy()
* in the pool. Call this method from the destroy() method of the servlet. * method of the servlet.
*/ * <p/>
* Multi-phase shutdown having following sequence:
/** * </p>
* Multi-phase shutdown. having following sequence: * <ol>
* <OL> * <li><code>getConnection()</code> will refuse to return connections.</li>
* <LI><code>getConnection()</code> will refuse to return connections. * <li>The housekeeping thread is shut down.<br>
* <LI>The housekeeping thread is shut down.<br> * Up to the time of <code>millis</code> milliseconds after shutdown of the housekeeping thread,
* Up to the time of <code>millis</code> milliseconds after shutdown of * <code>freeConnection()</code> can still be called to return used connections.<br>
* the housekeeping thread, <code>freeConnection()</code> can still be * After <code>millis</code> milliseconds after the shutdown of the housekeeping thread, all connections in the pool
* called to return used connections. * are closed.</li>
* <LI>After <code>millis</code> milliseconds after the shutdown of the * <li>If any connections were in use while being closed then a <code>SQLException</code> is thrown.</li>
* housekeeping thread, all connections in the pool are closed. * <li>The log is closed.</li>
* <LI>If any connections were in use while being closed then a * </ol>
* <code>SQLException</code> is thrown. * <p/>
* <LI>The log is closed.
* </OL><br>
* Call this method from a servlet destroy() method. * Call this method from a servlet destroy() method.
* *
* @param millis the time to wait in milliseconds. * @param millis the time to wait in milliseconds.
* @throws SQLException if connections were in use after *
* <code>millis</code>. * @throws SQLException if connections were in use after <code>millis</code>.
*/ */
public void destroy(int millis) throws SQLException { public void destroy(int millis) throws SQLException {
@ -520,11 +514,10 @@ public class SimplePool implements Runnable {
/** /**
* Less safe shutdown. Uses default timeout value. * Less safe shutdown. Uses default timeout value.
* This method simply calls the <code>destroy()</code> method * <p/>
* with a <code>millis</code> * This method simply calls the <code>destroy()</code> method with a <code>millis</code> value of 10000 (10 seconds)
* value of 10000 (10 seconds) and ignores <code>SQLException</code> * and ignores <code>SQLException</code> thrown by that method.
* thrown by that method.
* *
* @see #destroy(int) * @see #destroy(int)
*/ */
@ -539,13 +532,11 @@ public class SimplePool implements Runnable {
/** /**
* Returns the number of connections in use. * Returns the number of connections in use.
* <p/>
* This method could be reduced to return a counter that is maintained by all methods that update connStatus.
* However, it is more efficient to do it this way because: Updating the counter would put an additional burden on
* the most frequently used methods; in comparison, this method is rarely used (although essential).
*/ */
// This method could be reduced to return a counter that is
// maintained by all methods that update connStatus.
// However, it is more efficient to do it this way because:
// Updating the counter would put an additional burden on the most
// frequently used methods; in comparison, this method is
// rarely used (although essential).
public int getUseCount() { public int getUseCount() {
int useCount = 0; int useCount = 0;
synchronized (connStatus) { synchronized (connStatus) {

View file

@ -1,276 +1,407 @@
/** /**
* $Source$ * $Source$ $Revision: 1.1
* $Revision$ * $ $Date$ Copyright (c) 2004, Russell Beattie (http://www.russellbeattie.com/) All rights
* $Date$ * reserved. Copyright (c) 2004, Erik C. Thauvin (http://www.thauvin.net/erik/) All rights reserved. Redistribution
* * and use in source and binary forms, with or without modification, are permitted provided that the following
* Copyright (c) 2004, Russell Beattie (http://www.russellbeattie.com/) * conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions
* All rights reserved. * and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list
* * of conditions and the following disclaimer in the documentation and/or other materials provided with the
* Copyright (c) 2004, Erik C. Thauvin (http://www.thauvin.net/erik/) * distribution. Neither the name of the authors nor the names of its contributors may be used to endorse or promote
* All rights reserved. * products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE
* * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* Redistribution and use in source and binary forms, with or without * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* modification, are permitted provided that the following conditions are * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* met: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* Redistributions of source code must retain the above copyright notice, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* this list of conditions and the following disclaimer. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the authors nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package net.java.dev.simplepool; package net.java.dev.simplepool;
import java.sql.*; import java.sql.*;
import java.util.Map; import java.util.Map;
/** /**
* SimplePoolConnection * Basic implementation of <code>javax.sql.Connnection</code>.
* *
* @author Russell Beattie * @author <a href="http://www.russellbeattie.com/">Russell Beattie</a>
* @author Erik C. Thauvin * @author <a href="http://www.thauvin.net/erik/">Erik C. Thauvin</a>
* @version $Revision$, $Date$ * @version $Revision$, $Date$
* @since 1.0 * @since 1.0
*/ */
public class SimplePoolConnection implements Connection { public class SimplePoolConnection implements Connection {
protected Connection conn; private Connection conn;
protected SimplePool broker; private SimplePool broker;
/**
* Creates a new SimplePoolConnection object.
*
* @param broker The connection pool.
*/
public SimplePoolConnection(SimplePool broker) { public SimplePoolConnection(SimplePool broker) {
this.broker = broker; this.broker = broker;
conn = broker.getConnection(); conn = broker.getConnection();
} }
public void setAutoCommit(boolean arg0) /**
* See {@link java.sql.Connection#setAutoCommit(boolean)}.
*
* @see java.sql.Connection#setAutoCommit(boolean)
*/
public void setAutoCommit(boolean autoCommit)
throws SQLException { throws SQLException {
conn.setAutoCommit(arg0); conn.setAutoCommit(autoCommit);
} }
/**
* See {@link java.sql.Connection#getAutoCommit}.
*
* @see java.sql.Connection#getAutoCommit
*/
public boolean getAutoCommit() public boolean getAutoCommit()
throws SQLException { throws SQLException {
return conn.getAutoCommit(); return conn.getAutoCommit();
} }
/**
public void setCatalog(String arg0) * See {@link java.sql.Connection#setCatalog(String)}.
*
* @see java.sql.Connection#setCatalog(String)
*/
public void setCatalog(String catalog)
throws SQLException { throws SQLException {
conn.setCatalog(arg0); conn.setCatalog(catalog);
} }
/**
* See {@link java.sql.Connection#getCatalog()}.
*
* @see java.sql.Connection#getCatalog()
*/
public String getCatalog() public String getCatalog()
throws SQLException { throws SQLException {
return conn.getCatalog(); return conn.getCatalog();
} }
/**
* See {@link java.sql.Connection#isClosed}.
*
* @see java.sql.Connection#isClosed
*/
public boolean isClosed() public boolean isClosed()
throws SQLException { throws SQLException {
return conn.isClosed(); return conn.isClosed();
} }
/**
public void setHoldability(int arg0) * See {@link java.sql.Connection#setHoldability(int)}.
*
* @see java.sql.Connection#setHoldability(int)
*/
public void setHoldability(int holdability)
throws SQLException { throws SQLException {
conn.setHoldability(arg0); conn.setHoldability(holdability);
} }
/**
* See {@link java.sql.Connection#getHoldability}.
*
* @see java.sql.Connection#getHoldability
*/
public int getHoldability() public int getHoldability()
throws SQLException { throws SQLException {
return conn.getHoldability(); return conn.getHoldability();
} }
/**
* See {@link java.sql.Connection#getMetaData}.
*
* @see java.sql.Connection#getMetaData
*/
public DatabaseMetaData getMetaData() public DatabaseMetaData getMetaData()
throws SQLException { throws SQLException {
return conn.getMetaData(); return conn.getMetaData();
} }
/**
public void setReadOnly(boolean arg0) * See {@link java.sql.Connection#setReadOnly(boolean)}.
*
* @see java.sql.Connection#setReadOnly(boolean)
*/
public void setReadOnly(boolean readOnly)
throws SQLException { throws SQLException {
conn.setReadOnly(arg0); conn.setReadOnly(readOnly);
} }
/**
* See {@link java.sql.Connection#isReadOnly}.
*
* @see java.sql.Connection#isReadOnly
*/
public boolean isReadOnly() public boolean isReadOnly()
throws SQLException { throws SQLException {
return conn.isReadOnly(); return conn.isReadOnly();
} }
/**
* See {@link java.sql.Connection#setSavepoint}.
*
* @see java.sql.Connection#setSavepoint
*/
public Savepoint setSavepoint() public Savepoint setSavepoint()
throws SQLException { throws SQLException {
return conn.setSavepoint(); return conn.setSavepoint();
} }
/**
public Savepoint setSavepoint(String arg0) * See {@link java.sql.Connection#setSavepoint(String)}.
*
* @see java.sql.Connection#setSavepoint(String)
*/
public Savepoint setSavepoint(String savepoint)
throws SQLException { throws SQLException {
return conn.setSavepoint(arg0); return conn.setSavepoint(savepoint);
} }
/**
public void setTransactionIsolation(int arg0) * See {@link java.sql.Connection#setTransactionIsolation(int)}.
*
* @see java.sql.Connection#setTransactionIsolation(int)
*/
public void setTransactionIsolation(int level)
throws SQLException { throws SQLException {
conn.setTransactionIsolation(arg0); conn.setTransactionIsolation(level);
} }
/**
* See {@link java.sql.Connection#getTransactionIsolation}.
*
* @see java.sql.Connection#getTransactionIsolation
*/
public int getTransactionIsolation() public int getTransactionIsolation()
throws SQLException { throws SQLException {
return conn.getTransactionIsolation(); return conn.getTransactionIsolation();
} }
/**
public void setTypeMap(Map arg0) * See {@link java.sql.Connection#setTypeMap(Map)}.
*
* @see java.sql.Connection#setTypeMap(Map)
*/
public void setTypeMap(Map map)
throws SQLException { throws SQLException {
conn.setTypeMap(arg0); conn.setTypeMap(map);
} }
/**
* See {@link java.sql.Connection#getTypeMap}.
*
* @see java.sql.Connection#getTypeMap
*/
public Map getTypeMap() public Map getTypeMap()
throws SQLException { throws SQLException {
return conn.getTypeMap(); return conn.getTypeMap();
} }
/**
* See {@link java.sql.Connection#getWarnings}.
*
* @see java.sql.Connection#getWarnings
*/
public SQLWarning getWarnings() public SQLWarning getWarnings()
throws SQLException { throws SQLException {
return conn.getWarnings(); return conn.getWarnings();
} }
/**
* See {@link java.sql.Connection#clearWarnings}.
*
* @see java.sql.Connection#clearWarnings
*/
public void clearWarnings() public void clearWarnings()
throws SQLException { throws SQLException {
conn.clearWarnings(); conn.clearWarnings();
} }
/**
* See {@link java.sql.Connection#close}.
*
* @see java.sql.Connection#close
*/
public void close() public void close()
throws SQLException { throws SQLException {
broker.freeConnection(conn); broker.freeConnection(conn);
} }
/**
* See {@link java.sql.Connection#commit}.
*
* @see java.sql.Connection#commit
*/
public void commit() public void commit()
throws SQLException { throws SQLException {
conn.commit(); conn.commit();
} }
/**
* See {@link java.sql.Connection#createStatement}.
*
* @see java.sql.Connection#createStatement
*/
public Statement createStatement() public Statement createStatement()
throws SQLException { throws SQLException {
return conn.createStatement(); return conn.createStatement();
} }
/**
public Statement createStatement(int arg0, int arg1) * See {@link java.sql.Connection#createStatement(int, int)}.
*
* @see java.sql.Connection#createStatement(int, int)
*/
public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException { throws SQLException {
return conn.createStatement(arg0, arg1); return conn.createStatement(resultSetType, resultSetConcurrency);
} }
/**
public Statement createStatement(int arg0, int arg1, int arg2) * See {@link java.sql.Connection#createStatement(int, int, int)}.
*
* @see java.sql.Connection#createStatement(int, int, int)
*/
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException { throws SQLException {
return conn.createStatement(arg0, arg1, arg2); return conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
} }
/**
public String nativeSQL(String arg0) * See {@link java.sql.Connection#nativeSQL(String)}.
*
* @see java.sql.Connection#nativeSQL(String)
*/
public String nativeSQL(String sql)
throws SQLException { throws SQLException {
return conn.nativeSQL(arg0); return conn.nativeSQL(sql);
} }
/**
public CallableStatement prepareCall(String arg0) * See {@link java.sql.Connection#prepareCall(String)}.
*
* @see java.sql.Connection#prepareCall(String)
*/
public CallableStatement prepareCall(String sql)
throws SQLException { throws SQLException {
return conn.prepareCall(arg0); return conn.prepareCall(sql);
} }
/**
public CallableStatement prepareCall(String arg0, int arg1, int arg2) * See {@link java.sql.Connection#prepareCall(String, int, int)}.
*
* @see java.sql.Connection#prepareCall(String, int, int)
*/
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException { throws SQLException {
return conn.prepareCall(arg0, arg1, arg2); return conn.prepareCall(sql, resultSetType, resultSetConcurrency);
} }
/**
public CallableStatement prepareCall(String arg0, int arg1, int arg2, int arg3) * See {@link java.sql.Connection#prepareCall(String, int, int, int)}.
*
* @see java.sql.Connection#prepareCall(String, int, int, int)
*/
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException { throws SQLException {
return conn.prepareCall(arg0, arg1, arg2, arg3); return conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
} }
/**
public PreparedStatement prepareStatement(String arg0) * See {@link java.sql.Connection#prepareStatement(String)}.
*
* @see java.sql.Connection#prepareStatement(String)
*/
public PreparedStatement prepareStatement(String sql)
throws SQLException { throws SQLException {
return conn.prepareStatement(arg0); return conn.prepareStatement(sql);
} }
/**
public PreparedStatement prepareStatement(String arg0, int arg1) * See {@link java.sql.Connection#prepareStatement(String)}.
*
* @see java.sql.Connection#prepareStatement(String)
*/
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
throws SQLException { throws SQLException {
return conn.prepareStatement(arg0); return conn.prepareStatement(sql);
} }
/**
public PreparedStatement prepareStatement(String arg0, int arg1, int arg2) * See {@link java.sql.Connection#prepareStatement(String, int, int)}.
*
* @see java.sql.Connection#prepareStatement(String, int, int)
*/
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException { throws SQLException {
return conn.prepareStatement(arg0, arg1, arg2); return conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
} }
/**
public PreparedStatement prepareStatement(String arg0, int arg1, int arg2, int arg3) * See {@link java.sql.Connection#prepareStatement(String, int, int, int)}.
*
* @see java.sql.Connection#prepareStatement(String, int, int, int)
*/
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException { throws SQLException {
return conn.prepareStatement(arg0, arg1, arg2, arg3); return conn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
} }
/**
public PreparedStatement prepareStatement(String arg0, int[] arg1) * See {@link java.sql.Connection#prepareStatement(String, int[])}.
*
* @see java.sql.Connection#prepareStatement(String, int[])
*/
public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
throws SQLException { throws SQLException {
return conn.prepareStatement(arg0, arg1); return conn.prepareStatement(sql, columnIndexes);
} }
/**
public PreparedStatement prepareStatement(String arg0, String[] arg1) * See {@link java.sql.Connection#prepareStatement(String, String[])}.
*
* @see java.sql.Connection#prepareStatement(String, String[])
*/
public PreparedStatement prepareStatement(String sql, String[] columnNames)
throws SQLException { throws SQLException {
return conn.prepareStatement(arg0, arg1); return conn.prepareStatement(sql, columnNames);
} }
/**
public void releaseSavepoint(Savepoint arg0) * See {@link java.sql.Connection#releaseSavepoint(Savepoint)}.
*
* @see java.sql.Connection#releaseSavepoint(Savepoint)
*/
public void releaseSavepoint(Savepoint savepoint)
throws SQLException { throws SQLException {
conn.releaseSavepoint(arg0); conn.releaseSavepoint(savepoint);
} }
/**
* See {@link java.sql.Connection#rollback}.
*
* @see java.sql.Connection#rollback
*/
public void rollback() public void rollback()
throws SQLException { throws SQLException {
conn.rollback(); conn.rollback();
} }
/**
public void rollback(Savepoint arg0) * See {@link java.sql.Connection#rollback(Savepoint)}.
*
* @see java.sql.Connection#rollback(Savepoint)
*/
public void rollback(Savepoint savepoint)
throws SQLException { throws SQLException {
conn.rollback(arg0); conn.rollback(savepoint);
} }
} }

View file

@ -44,17 +44,16 @@ import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
/** /**
* SimplePoolDataSource * Basic implementation of <code>javax.sql.DataSource</code>.
* *
* @author Russell Beattie * @author <a href="http://www.russellbeattie.com/">Russell Beattie</a>
* @author Erik C. Thauvin * @author <a href="http://www.thauvin.net/erik/">Erik C. Thauvin</a>
* @version $Revision$, $Date$ * @version $Revision$, $Date$
* @since 1.0 * @since 1.0
*/ */
public class SimplePoolDataSource implements DataSource { public class SimplePoolDataSource implements DataSource {
protected PrintWriter logWriter = new PrintWriter(System.out); protected PrintWriter logWriter = new PrintWriter(System.out);
protected SimplePool broker = null; protected SimplePool broker = null;
private String driver = ""; private String driver = "";
@ -66,11 +65,12 @@ public class SimplePoolDataSource implements DataSource {
private String maxConnTime = ""; private String maxConnTime = "";
private String maxCheckoutSeconds = ""; private String maxCheckoutSeconds = "";
public SimplePoolDataSource() { /**
; * Initializes the connection pool.
} *
* @throws Exception if the pool could not be intialized.
public void init() throws Exception { */
protected void init() throws Exception {
broker = new SimplePool(driver, jdbcUrl, user, password, broker = new SimplePool(driver, jdbcUrl, user, password,
Integer.parseInt(minConns), Integer.parseInt(maxConns), Integer.parseInt(minConns), Integer.parseInt(maxConns),
@ -78,6 +78,11 @@ public class SimplePoolDataSource implements DataSource {
} }
/**
* See {@link javax.sql.DataSource#getConnection()}.
*
* @see javax.sql.DataSource#getConnection()
*/
public Connection getConnection() throws SQLException { public Connection getConnection() throws SQLException {
if (broker == null) { if (broker == null) {
@ -92,96 +97,169 @@ public class SimplePoolDataSource implements DataSource {
} }
public Connection getConnection(String user, String password) /**
* See {@link javax.sql.DataSource#getConnection(String, String)}.
*
* @see javax.sql.DataSource#getConnection(String, String)
*/
public Connection getConnection(String username, String password)
throws SQLException { throws SQLException {
throw new SQLException("Not supported in this DataSource."); throw new SQLException("Not supported in this DataSource.");
} }
public void setLogWriter(PrintWriter output) throws SQLException { /**
logWriter = output; * See {@link javax.sql.DataSource#setLogWriter(PrintWriter)}.
*
* @see javax.sql.DataSource#setLogWriter(PrintWriter)
*/
public void setLogWriter(PrintWriter out) throws SQLException {
logWriter = out;
} }
/**
* See {@link javax.sql.DataSource#getLogWriter}.
*
* @see javax.sql.DataSource#getLogWriter
*/
public PrintWriter getLogWriter() { public PrintWriter getLogWriter() {
return logWriter; return logWriter;
} }
/**
* See {@link javax.sql.DataSource#setLoginTimeout(int)}.
*
* @see javax.sql.DataSource#setLoginTimeout(int)
*/
public void setLoginTimeout(int seconds) throws SQLException { public void setLoginTimeout(int seconds) throws SQLException {
} }
/**
* See {@link javax.sql.DataSource#getLoginTimeout}.
*
* @see javax.sql.DataSource#getLoginTimeout
*/
public int getLoginTimeout() { public int getLoginTimeout() {
return 0; return 0;
} }
/**
* Closes the connection pool.
*/
public void close() { public void close() {
broker.destroy(); broker.destroy();
broker = null; broker = null;
} }
/**
// GET/SETs * Sets the JDBC driver.
*/
public void setDriver(String val) { public void setDriver(String val) {
driver = val; driver = val;
} }
/**
* Gets the JDBC driver.
*/
public String getDriver() { public String getDriver() {
return driver; return driver;
} }
/**
* Sets the JDBC connect string.
*/
public void setJdbcUrl(String val) { public void setJdbcUrl(String val) {
jdbcUrl = val; jdbcUrl = val;
} }
/**
* Sets the JDBC connect string.
*/
public String getJdbcUrl() { public String getJdbcUrl() {
return jdbcUrl; return jdbcUrl;
} }
/**
* Sets the database login name.
*/
public void setUser(String val) { public void setUser(String val) {
user = val; user = val;
} }
/**
* Gets the database login name.
*/
public String getUser() { public String getUser() {
return user; return user;
} }
/**
* Sets the database password.
*/
public void setPassword(String val) { public void setPassword(String val) {
password = val; password = val;
} }
/**
* Gets the database password.
*/
public String getPassword() { public String getPassword() {
return password; return password;
} }
/**
* Sets the minimum number of connections to start with.
*/
public void setMinConns(String val) { public void setMinConns(String val) {
minConns = val; minConns = val;
} }
/**
* Gets the minimum number of connections to start with.
*/
public String getMinConns() { public String getMinConns() {
return minConns; return minConns;
} }
/**
* Sets the maximum number of connections in dynamic pool.
*/
public void setMaxConns(String val) { public void setMaxConns(String val) {
maxConns = val; maxConns = val;
} }
/**
* Gets the maximum number of connections in dynamic pool.
*/
public String getMaxConns() { public String getMaxConns() {
return maxConns; return maxConns;
} }
/**
* Sets the time in days between connection resets.
*/
public void setMaxConnTime(String val) { public void setMaxConnTime(String val) {
maxConnTime = val; maxConnTime = val;
} }
/**
* Gets the time in days between connection resets.
*/
public String getMaxConnTime() { public String getMaxConnTime() {
return maxConnTime; return maxConnTime;
} }
/**
* Sets the max time a connection can be checked out before being recycled.
*/
public void setMaxCheckoutSeconds(String val) { public void setMaxCheckoutSeconds(String val) {
maxCheckoutSeconds = val; maxCheckoutSeconds = val;
} }
/**
* Sets the max time a connection can be checked out before being recycled.
*/
public String getMaxCheckoutSeconds() { public String getMaxCheckoutSeconds() {
return maxCheckoutSeconds; return maxCheckoutSeconds;
} }

View file

@ -45,15 +45,29 @@ import javax.naming.spi.ObjectFactory;
import java.util.Hashtable; import java.util.Hashtable;
/** /**
* SimplePoolDataSourceFactory * JNDI object factory that creates an instance of {@link SimplePoolDataSource}.
* *
* @author Russell Beattie * @author <a href="http://www.russellbeattie.com/">Russell Beattie</a>
* @author Erik C. Thauvin * @author <a href="http://www.thauvin.net/erik/">Erik C. Thauvin</a>
* @version $Revision$, $Date$ * @version $Revision$, $Date$
* @since 1.0 * @since 1.0
*/ */
public class SimplePoolDataSourceFactory implements ObjectFactory { public class SimplePoolDataSourceFactory implements ObjectFactory {
/**
* Creates a SimplePool DataSource factory.
*
* @param obj The object containing location or reference information that is used in creating the
* <code>DataSource</code>.
* @param name The name of this object relative to <code>ctx</code>, or null if no name is specified.
* @param ctx The context relative to which the <code>name</code> parameter is specified, or null if
* <code>name</code> is relative to the default initial context.
* @param env The possibly null environment that is used in creating the <code>DataSource</code>.
*
* @return The <code>DataSource</code>; null if it cannot be created.
*
* @throws Exception if the factory encountered an exception while attempting to create the <code>DataSource</code>,
* and no other object factories are to be tried.
*/
public Object getObjectInstance(Object obj, Name name, Context ctx, Hashtable env) public Object getObjectInstance(Object obj, Name name, Context ctx, Hashtable env)
throws Exception { throws Exception {
@ -62,13 +76,14 @@ public class SimplePoolDataSourceFactory implements ObjectFactory {
/* /*
driver: JDBC driver. e.g. 'oracle.jdbc.driver.OracleDriver'<br> driver: JDBC driver. e.g. 'oracle.jdbc.driver.OracleDriver'<br>
jdbcUrl: JDBC connect string. e.g. 'jdbc:oracle:thin:@203.92.21.109:1526:orcl'<br> jdbcUrl: JDBC connect string. e.g. 'jdbc:oracle:thin:@203.92.21.109:1526:orcl'<br>
user: Database login name. e.g. 'Scott'<br> user: Database login name. e.g. 'Scott'<br>
password: Database password. e.g. 'Tiger'<br> password: Database password. e.g. 'Tiger'<br>
minConns: Minimum number of connections to start with.<br> minConns: Minimum number of connections to start with.<br>
maxConns: Maximum number of connections in dynamic pool.<br> maxConns: Maximum number of connections in dynamic pool.<br>
maxConnTime: Time in days between connection resets. (Reset does a basic cleanup)<br> maxConnTime: Time in days between connection resets. (Reset does a basic cleanup)<br>
maxCheckoutSeconds: Max time a connection can be checked out before being recycled. Zero value turns option off, default is 60 seconds. maxCheckoutSeconds: Max time a connection can be checked out before being recycled. Zero value turns option
off, default is 60 seconds.
*/ */
String driver = (String) ref.get("driver").getContent(); String driver = (String) ref.get("driver").getContent();

View file

@ -1,40 +1,20 @@
/** /**
* $Source$ * $Source$ $Revision$
* $Revision$ * $Date$ Copyright (c) 2004, Russell Beattie (http://www.russellbeattie.com/) All rights
* $Date$ * reserved. Copyright (c) 2004, Erik C. Thauvin (http://www.thauvin.net/erik/) All rights reserved. Redistribution
* * and use in source and binary forms, with or without modification, are permitted provided that the following
* Copyright (c) 2004, Russell Beattie (http://www.russellbeattie.com/) * conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions
* All rights reserved. * and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list
* * of conditions and the following disclaimer in the documentation and/or other materials provided with the
* Copyright (c) 2004, Erik C. Thauvin (http://www.thauvin.net/erik/) * distribution. Neither the name of the authors nor the names of its contributors may be used to endorse or promote
* All rights reserved. * products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE
* * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* Redistribution and use in source and binary forms, with or without * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* modification, are permitted provided that the following conditions are * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* met: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* Redistributions of source code must retain the above copyright notice, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* this list of conditions and the following disclaimer. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the authors nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package net.java.dev.simplepool; package net.java.dev.simplepool;
@ -47,10 +27,10 @@ import javax.servlet.http.HttpServlet;
/** /**
* SimplePoolServlet * Creates a pool of database connection based on the specific intialization parameters.
* *
* @author Russell Beattie * @author <a href="http://www.russellbeattie.com/">Russell Beattie</a>
* @author Erik C. Thauvin * @author <a href="http://www.thauvin.net/erik/">Erik C. Thauvin</a>
* @version $Revision$, $Date$ * @version $Revision$, $Date$
* @since 1.0 * @since 1.0
*/ */
@ -59,12 +39,22 @@ public class SimplePoolServlet extends HttpServlet {
private static final Log log = LogFactory.getLog(SimplePoolServlet.class); private static final Log log = LogFactory.getLog(SimplePoolServlet.class);
private static SimplePoolDataSource dataSource; private static SimplePoolDataSource dataSource;
/**
* Closes the connection pool.
*
* @see javax.servlet.http.HttpServlet#destroy
*/
public void destroy() { public void destroy() {
if (dataSource != null) { if (dataSource != null) {
dataSource.close(); dataSource.close();
} }
} }
/**
* Initializes the connection pool.
*
* @see javax.servlet.http.HttpServlet#init(ServletConfig)
*/
public void init(ServletConfig servletConfig) public void init(ServletConfig servletConfig)
throws ServletException { throws ServletException {
log.info("Initializing " + servletConfig.getServletName() + " Servlet"); log.info("Initializing " + servletConfig.getServletName() + " Servlet");

View file

@ -0,0 +1,13 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>SimplePool</title>
</head>
<body>
This package contains a simple database connection pool implementation for web applications, specifically those using JSTL.
<p/>
The connection pool can be accessed via JNDI or through the {@link net.java.dev.simplepool.SimplePoolServlet}.
@since 1.0
</body>
</html>