Intial import.
This commit is contained in:
commit
bfc101df5b
12 changed files with 1453 additions and 0 deletions
7
.cvsignore
Normal file
7
.cvsignore
Normal file
|
@ -0,0 +1,7 @@
|
|||
*.ipr
|
||||
*.iml
|
||||
*.iws
|
||||
bin
|
||||
dist
|
||||
build
|
||||
classes
|
7
build.properties
Normal file
7
build.properties
Normal file
|
@ -0,0 +1,7 @@
|
|||
appname=simplepool
|
||||
|
||||
path.classes=build
|
||||
path.src=src
|
||||
path.dist=dist
|
||||
path.lib=lib
|
||||
|
36
build.xml
Normal file
36
build.xml
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project name="simplepool" default="build" basedir=".">
|
||||
<property file="${basedir}/build.properties" />
|
||||
<path id="path.source">
|
||||
<pathelement location="${path.src}" />
|
||||
</path>
|
||||
<path id="path.class">
|
||||
<fileset dir="${path.lib}">
|
||||
<include name="**/*.jar"/>
|
||||
</fileset>
|
||||
</path>
|
||||
<target name="init">
|
||||
<tstamp />
|
||||
<mkdir dir="${path.classes}"/>
|
||||
<mkdir dir="${path.dist}"/>
|
||||
</target>
|
||||
<target name="compile" depends="init" description="Compiles sources">
|
||||
<mkdir dir="${path.classes}" />
|
||||
<javac sourcepathref="path.source" classpathref="path.class" destdir="${path.classes}" debug="off" deprecation="on">
|
||||
<src refid="path.source" />
|
||||
</javac>
|
||||
</target>
|
||||
<target name="build" depends="clean,compile,jar" description="Rebuilds project" />
|
||||
<target name="jar" depends="compile" description="Builds the JAR">
|
||||
<copy todir="${path.classes}">
|
||||
<fileset dir="${path.src}" includes="*.properties" />
|
||||
</copy>
|
||||
<jar destfile="${basedir}/${path.dist}/${appname}.jar" basedir="${path.classes}" excludes="**/.dependency-info/**" />
|
||||
</target>
|
||||
<target name="clean" depends="init" description="Removes classses and javadoc">
|
||||
<delete quiet="true" includeEmptyDirs="true">
|
||||
<fileset dir="${path.classes}" includes="*,*/**"/>
|
||||
<fileset dir="${path.dist}" includes="*,*/**"/>
|
||||
</delete>
|
||||
</target>
|
||||
</project>
|
107
conf/server.xml
Normal file
107
conf/server.xml
Normal file
|
@ -0,0 +1,107 @@
|
|||
|
||||
|
||||
<Server port="8005" shutdown="SHUTDOWN" debug="0">
|
||||
|
||||
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" debug="0"/>
|
||||
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" debug="0"/>
|
||||
|
||||
<GlobalNamingResources>
|
||||
|
||||
<Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase"/>
|
||||
|
||||
<ResourceParams name="UserDatabase">
|
||||
<parameter>
|
||||
<name>factory</name>
|
||||
<value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>pathname</name>
|
||||
<value>conf/tomcat-users.xml</value>
|
||||
</parameter>
|
||||
</ResourceParams>
|
||||
|
||||
|
||||
<Resource name="jdbc/PooledDS" type="javax.sql.DataSource" auth="Container"/>
|
||||
|
||||
<ResourceParams name="jdbc/PooledDS">
|
||||
<parameter>
|
||||
<name>factory</name>
|
||||
<value>net.java.dev.simplepool.SimplePoolDataSourceFactory</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>driver</name>
|
||||
<value>com.mysql.jdbc.Driver</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>user</name>
|
||||
<value>dbuser</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>password</name>
|
||||
<value>test</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>jdbcUrl</name>
|
||||
<value>jdbc:mysql://localhost:3306/dbname</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>minConns</name>
|
||||
<value>8</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>maxConns</name>
|
||||
<value>30</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>maxConnTime</name>
|
||||
<value>1</value>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>maxCheckoutSeconds</name>
|
||||
<value>60</value>
|
||||
</parameter>
|
||||
|
||||
</ResourceParams>
|
||||
|
||||
</GlobalNamingResources>
|
||||
|
||||
<Service name="Catalina">
|
||||
|
||||
<Connector port="80"
|
||||
maxThreads="150"
|
||||
minSpareThreads="25"
|
||||
maxSpareThreads="75"
|
||||
enableLookups="false"
|
||||
redirectPort="8443"
|
||||
acceptCount="100"
|
||||
debug="0"
|
||||
connectionTimeout="20000"
|
||||
proxyPort="80"
|
||||
/>
|
||||
|
||||
|
||||
<Engine name="Catalina" defaultHost="www.waveblog.com" debug="0">
|
||||
|
||||
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
|
||||
debug="1" resourceName="UserDatabase"/>
|
||||
|
||||
<Host name="localhost"
|
||||
debug="1" appBase="webapps" unpackWARs="true" autoDeploy="true"
|
||||
xmlValidation="false" xmlNamespaceAware="false">
|
||||
|
||||
<Context path="" docBase="ROOT" debug="0" reloadable="true">
|
||||
<ResourceLink name="jdbc/PooledDS" global="jdbc/PooledDS" type="javax.sql.DataSource" />
|
||||
</Context>
|
||||
|
||||
<Valve className="org.apache.catalina.valves.AccessLogValve"
|
||||
directory="logs" prefix="waveblog" suffix=".log"
|
||||
pattern="combined" resolveHosts="false"/>
|
||||
|
||||
</Host>
|
||||
|
||||
|
||||
</Engine>
|
||||
|
||||
</Service>
|
||||
|
||||
</Server>
|
21
conf/test.jsp
Normal file
21
conf/test.jsp
Normal file
|
@ -0,0 +1,21 @@
|
|||
<% out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); %>
|
||||
<%@ page contentType="text/xml;charset=UTF-8"%>
|
||||
<%@ page session="false"%>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
|
||||
|
||||
<sql:setDataSource dataSource="jdbc/PooledDS" />
|
||||
|
||||
<sql:query var="page" maxRows="30">
|
||||
select * from entry
|
||||
</sql:query>
|
||||
|
||||
<document>
|
||||
<c:forEach var="row" items="${page.rows}" varStatus="counter">
|
||||
<entry>
|
||||
<count><c:out value="${counter.count}"/></count>
|
||||
<id><c:out value="${row.id}"/></id>
|
||||
<title><c:out value="${row.name}"/></title>
|
||||
</entry>
|
||||
</c:forEach>
|
||||
</document>
|
BIN
lib/commons-logging-api.jar
Normal file
BIN
lib/commons-logging-api.jar
Normal file
Binary file not shown.
BIN
lib/servlet.jar
Normal file
BIN
lib/servlet.jar
Normal file
Binary file not shown.
568
src/net/java/dev/simplepool/SimplePool.java
Normal file
568
src/net/java/dev/simplepool/SimplePool.java
Normal file
|
@ -0,0 +1,568 @@
|
|||
/**
|
||||
* $Source$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (c) 2002, Marc A. Mnich (http://www.javaexchange.com/)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Copyright (c) 2004, Russell Beattie (http://www.russellbeattie.com/)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Copyright (c) 2004, Erik C. Thauvin (http://www.thauvin.net/erik/)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License, please see:
|
||||
*
|
||||
* http://www.javaexchange.com/GPL.html
|
||||
*/
|
||||
package net.java.dev.simplepool;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* SimplePool
|
||||
*
|
||||
* @author Marc A. Mnich
|
||||
* @author Russell Beattie
|
||||
* @author Erik C. Thauvin
|
||||
* @version $Revision$, $Date$
|
||||
* @since 1.0
|
||||
*/
|
||||
public class SimplePool implements Runnable {
|
||||
|
||||
private static final Log log = LogFactory.getLog(SimplePool.class);
|
||||
|
||||
private Thread runner;
|
||||
|
||||
private Connection[] connPool;
|
||||
private int[] connStatus;
|
||||
|
||||
private long[] connLockTime;
|
||||
private long[] connCreateDate;
|
||||
private String[] connID;
|
||||
private String driver;
|
||||
private String jdbcUrl;
|
||||
private String user;
|
||||
private String password;
|
||||
private int currConnections;
|
||||
private int connLast;
|
||||
private int maxConns;
|
||||
private int maxConnMSec;
|
||||
private int maxCheckoutSeconds = 60;
|
||||
//available: set to false on destroy, checked by getConnection()
|
||||
private boolean available = true;
|
||||
|
||||
private SQLWarning currSQLWarning;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new Connection Broker<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>
|
||||
* user: Database login name. e.g. 'Scott'<br>
|
||||
* password: Database password. e.g. 'Tiger'<br>
|
||||
* minConns: Minimum number of connections to start with.<br>
|
||||
* maxConns: Maximum number of connections in dynamic pool.<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.
|
||||
*/
|
||||
public SimplePool(String driver, String jdbcUrl, String user,
|
||||
String password, int minConns, int maxConns, double maxConnTime, int maxCheckoutSeconds)
|
||||
throws IOException {
|
||||
|
||||
this.connPool = new Connection[maxConns];
|
||||
this.connStatus = new int[maxConns];
|
||||
this.connLockTime = new long[maxConns];
|
||||
this.connCreateDate = new long[maxConns];
|
||||
this.connID = new String[maxConns];
|
||||
this.currConnections = minConns;
|
||||
this.maxConns = maxConns;
|
||||
this.driver = driver;
|
||||
this.jdbcUrl = jdbcUrl;
|
||||
this.user = user;
|
||||
this.password = password;
|
||||
this.maxCheckoutSeconds = maxCheckoutSeconds;
|
||||
this.maxConnMSec = (int) (maxConnTime * 86400000.0); //86400 sec/day
|
||||
if (this.maxConnMSec < 30000) { // Recycle no less than 30 seconds.
|
||||
this.maxConnMSec = 30000;
|
||||
}
|
||||
|
||||
log.info("-----------------------------------------");
|
||||
log.info("Starting Connection Pool:");
|
||||
log.info("driver = " + driver);
|
||||
log.info("jdbcUrl = " + jdbcUrl);
|
||||
log.info("user = " + user);
|
||||
log.info("minconnections = " + minConns);
|
||||
log.info("maxconnections = " + maxConns);
|
||||
log.info("Total refresh interval = " + maxConnTime + " days");
|
||||
log.info("maxCheckoutSeconds = " + maxCheckoutSeconds);
|
||||
log.info("-----------------------------------------");
|
||||
|
||||
|
||||
// Initialize the pool of connections with the mininum connections:
|
||||
// Problems creating connections may be caused during reboot when the
|
||||
// servlet is started before the database is ready. Handle this
|
||||
// by waiting and trying again. The loop allows 5 minutes for
|
||||
// db reboot.
|
||||
boolean connectionsSucceeded = false;
|
||||
int dbLoop = 20;
|
||||
|
||||
try {
|
||||
for (int i = 1; i < dbLoop; i++) {
|
||||
try {
|
||||
for (int j = 0; j < currConnections; j++) {
|
||||
createConn(j);
|
||||
}
|
||||
connectionsSucceeded = true;
|
||||
break;
|
||||
} catch (SQLException e) {
|
||||
|
||||
log.error("Attempt (" + String.valueOf(i) +
|
||||
" of " + String.valueOf(dbLoop) +
|
||||
") failed to create new connections set at startup.", e);
|
||||
log.warn("Will try again in 15 seconds...");
|
||||
|
||||
try {
|
||||
Thread.sleep(15000L);
|
||||
} catch (InterruptedException ignore) {
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!connectionsSucceeded) { // All attempts at connecting to db exhausted
|
||||
throw new IOException("All attempts at connecting to Database exhausted.");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.fatal(e.getMessage(), e);
|
||||
throw new IOException(e.getMessage());
|
||||
}
|
||||
|
||||
// Fire up the background housekeeping thread
|
||||
runner = new Thread(this);
|
||||
runner.start();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Housekeeping thread. Runs in the background with low CPU overhead.
|
||||
* Connections are checked for warnings and closure and are periodically
|
||||
* restarted.
|
||||
* This thread is a catchall for corrupted
|
||||
* connections and prevents the buildup of open cursors. (Open cursors
|
||||
* result when the application fails to close a Statement).
|
||||
* This method acts as fault tolerance for bad connection/statement programming.
|
||||
*/
|
||||
public void run() {
|
||||
boolean forever = true;
|
||||
Statement stmt = null;
|
||||
long maxCheckoutMillis = (long) (maxCheckoutSeconds * 1000);
|
||||
|
||||
while (forever) {
|
||||
|
||||
// Get any Warnings on connections and print to event file
|
||||
for (int i = 0; i < currConnections; i++) {
|
||||
try {
|
||||
currSQLWarning = connPool[i].getWarnings();
|
||||
if (currSQLWarning != null) {
|
||||
|
||||
log.debug("Warnings on connection [" + String.valueOf(i) + "]: " + currSQLWarning);
|
||||
|
||||
connPool[i].clearWarnings();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
log.debug("Cannot access connection [" + String.valueOf(i) + "] warnings.", e);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < currConnections; i++) { // Do for each connection
|
||||
|
||||
long age = System.currentTimeMillis() - connCreateDate[i];
|
||||
|
||||
try { // Test the connection with createStatement call
|
||||
synchronized (connStatus) {
|
||||
if (connStatus[i] > 0) { // In use, catch it next time!
|
||||
|
||||
// Check the time it's been checked out and recycle
|
||||
long timeInUse = System.currentTimeMillis() -
|
||||
connLockTime[i];
|
||||
log.warn("Connection [" + i +
|
||||
"] in use for " + timeInUse +
|
||||
" ms.");
|
||||
if (maxCheckoutMillis != 0) {
|
||||
if (timeInUse > maxCheckoutMillis) {
|
||||
log.warn("Connection [" +
|
||||
i + "] failed to be returned in time. Recycling...");
|
||||
throw new SQLException();
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
connStatus[i] = 2; // Take offline (2 indicates housekeeping lock)
|
||||
}
|
||||
|
||||
|
||||
if (age > maxConnMSec) { // Force a reset at the max conn time
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
stmt = connPool[i].createStatement();
|
||||
connStatus[i] = 0; // Connection is O.K.
|
||||
log.trace("Connection [" + String.valueOf(i) + "] confirmed.");
|
||||
|
||||
// Some DBs return an object even if DB is shut down
|
||||
if (connPool[i].isClosed()) {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
|
||||
// Connection has a problem, restart it
|
||||
} catch (SQLException e) {
|
||||
|
||||
log.debug("Recycling connection [" + String.valueOf(i) + ']');
|
||||
try {
|
||||
connPool[i].close();
|
||||
} catch (SQLException e0) {
|
||||
log.warn("Can't close connection [" + String.valueOf(i) + "]. Might have been closed already. Trying to recycle anyway...", e);
|
||||
}
|
||||
|
||||
try {
|
||||
createConn(i);
|
||||
} catch (SQLException e1) {
|
||||
log.warn("Failed to create connection [" + String.valueOf(i) + ']', e1);
|
||||
|
||||
connStatus[i] = 0; // Can't open, try again next time
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
} catch (SQLException ignore) {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(20000L);
|
||||
} // Wait 20 seconds for next cycle
|
||||
catch (InterruptedException e) {
|
||||
// Returning from the run method sets the internal
|
||||
// flag referenced by Thread.isAlive() to false.
|
||||
// This is required because we don't use stop() to
|
||||
// shutdown this thread.
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // End run
|
||||
|
||||
/**
|
||||
* This method hands out the connections in round-robin order.
|
||||
* This prevents a faulty connection from locking
|
||||
* up an application entirely. A browser 'refresh' will
|
||||
* get the next connection while the faulty
|
||||
* connection is cleaned up by the housekeeping thread.
|
||||
* <p/>
|
||||
* If the min number of threads are ever exhausted, new
|
||||
* threads are added up the the max thread count.
|
||||
* Finally, if all threads are in use, this method waits
|
||||
* 2 seconds and tries again, up to ten times. After that, it
|
||||
* returns a null.
|
||||
*/
|
||||
public Connection getConnection() {
|
||||
|
||||
Connection conn = null;
|
||||
|
||||
if (available) {
|
||||
boolean gotOne = false;
|
||||
|
||||
for (int outerloop = 1; outerloop <= 10; outerloop++) {
|
||||
|
||||
try {
|
||||
int loop = 0;
|
||||
int roundRobin = connLast + 1;
|
||||
if (roundRobin >= currConnections) {
|
||||
roundRobin = 0;
|
||||
}
|
||||
|
||||
do {
|
||||
synchronized (connStatus) {
|
||||
if ((connStatus[roundRobin] < 1) &&
|
||||
(!connPool[roundRobin].isClosed())) {
|
||||
conn = connPool[roundRobin];
|
||||
connStatus[roundRobin] = 1;
|
||||
connLockTime[roundRobin] = System.currentTimeMillis();
|
||||
connLast = roundRobin;
|
||||
gotOne = true;
|
||||
break;
|
||||
} else {
|
||||
loop++;
|
||||
roundRobin++;
|
||||
if (roundRobin >= currConnections) {
|
||||
roundRobin = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
} while ((!gotOne) && (loop < currConnections));
|
||||
|
||||
} catch (SQLException e1) {
|
||||
log.debug(e1.getMessage(), e1);
|
||||
}
|
||||
|
||||
if (gotOne) {
|
||||
break;
|
||||
} else {
|
||||
synchronized (this) { // Add new connections to the pool
|
||||
if (currConnections < maxConns) {
|
||||
|
||||
try {
|
||||
createConn(currConnections);
|
||||
currConnections++;
|
||||
} catch (SQLException e) {
|
||||
log.error("Unable to create new connection.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(2000L);
|
||||
} catch (InterruptedException ignore) {
|
||||
;
|
||||
}
|
||||
|
||||
log.debug("Connections Exhausted. Will wait and try again in loop " +
|
||||
String.valueOf(outerloop));
|
||||
}
|
||||
|
||||
} // End of try 10 times loop
|
||||
|
||||
} else {
|
||||
log.debug("Unsuccessful getConnection() request during destroy()");
|
||||
} // End if(available)
|
||||
|
||||
log.debug("Handing out connection [" +
|
||||
idOfConnection(conn) + "]: " +
|
||||
(new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a")).format(new Date()));
|
||||
|
||||
return conn;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the local JDBC ID for a connection.
|
||||
*/
|
||||
public int idOfConnection(Connection conn) {
|
||||
int match = -1;
|
||||
String tag;
|
||||
|
||||
try {
|
||||
tag = conn.toString();
|
||||
} catch (NullPointerException e1) {
|
||||
tag = "none";
|
||||
}
|
||||
|
||||
for (int i = 0; i < currConnections; i++) {
|
||||
if (connID[i].equals(tag)) {
|
||||
match = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return match;
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees a connection. Replaces connection back into the main pool for
|
||||
* reuse.
|
||||
*/
|
||||
public String freeConnection(Connection conn) {
|
||||
String res = "";
|
||||
|
||||
int thisconn = idOfConnection(conn);
|
||||
if (thisconn >= 0) {
|
||||
connStatus[thisconn] = 0;
|
||||
res = "freed " + conn.toString();
|
||||
log.debug("Freed connection [" + String.valueOf(thisconn) + ']');
|
||||
} else {
|
||||
log.error("Could not free connection [" + String.valueOf(thisconn) + ']');
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the age of a connection -- the time since it was handed out to
|
||||
* an application.
|
||||
*/
|
||||
public long getAge(Connection conn) { // Returns the age of the connection in millisec.
|
||||
int thisconn = idOfConnection(conn);
|
||||
return System.currentTimeMillis() - connLockTime[thisconn];
|
||||
}
|
||||
|
||||
private void createConn(int i)
|
||||
throws SQLException {
|
||||
|
||||
Date now = new Date();
|
||||
|
||||
try {
|
||||
Class.forName(driver);
|
||||
|
||||
connPool[i] = DriverManager.getConnection
|
||||
(jdbcUrl, user, password);
|
||||
|
||||
connStatus[i] = 0;
|
||||
connID[i] = connPool[i].toString();
|
||||
connLockTime[i] = 0L;
|
||||
connCreateDate[i] = now.getTime();
|
||||
} catch (ClassNotFoundException e2) {
|
||||
log.debug("Error creating connection. The driver could not be loaded.", e2);
|
||||
}
|
||||
|
||||
log.debug("Opening connection [" + String.valueOf(i) +
|
||||
"]: " + connPool[i].toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuts down the housekeeping thread and closes all connections
|
||||
* in the pool. Call this method from the destroy() method of the servlet.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Multi-phase shutdown. having following sequence:
|
||||
* <OL>
|
||||
* <LI><code>getConnection()</code> will refuse to return connections.
|
||||
* <LI>The housekeeping thread is shut down.<br>
|
||||
* Up to the time of <code>millis</code> milliseconds after shutdown of
|
||||
* the housekeeping thread, <code>freeConnection()</code> can still be
|
||||
* called to return used connections.
|
||||
* <LI>After <code>millis</code> milliseconds after the shutdown of the
|
||||
* housekeeping thread, all connections in the pool are closed.
|
||||
* <LI>If any connections were in use while being closed then a
|
||||
* <code>SQLException</code> is thrown.
|
||||
* <LI>The log is closed.
|
||||
* </OL><br>
|
||||
* Call this method from a servlet destroy() method.
|
||||
*
|
||||
* @param millis the time to wait in milliseconds.
|
||||
* @throws SQLException if connections were in use after
|
||||
* <code>millis</code>.
|
||||
*/
|
||||
public void destroy(int millis) throws SQLException {
|
||||
|
||||
log.info("Shutting down SimplePool.");
|
||||
|
||||
// Checking for invalid negative arguments is not necessary,
|
||||
// Thread.join() does this already in runner.join().
|
||||
|
||||
// Stop issuing connections
|
||||
available = false;
|
||||
|
||||
// Shut down the background housekeeping thread
|
||||
runner.interrupt();
|
||||
|
||||
// Wait until the housekeeping thread has died.
|
||||
try {
|
||||
runner.join((long) millis);
|
||||
} catch (InterruptedException ignore) {
|
||||
;
|
||||
}
|
||||
|
||||
// The housekeeping thread could still be running
|
||||
// (e.g. if millis is too small). This case is ignored.
|
||||
// At worst, this method will throw an exception with the
|
||||
// clear indication that the timeout was too short.
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
// Wait for freeConnection() to return any connections
|
||||
// that are still used at this time.
|
||||
int useCount;
|
||||
while ((useCount = getUseCount()) > 0 && System.currentTimeMillis() - startTime <= millis) {
|
||||
try {
|
||||
Thread.sleep(500L);
|
||||
} catch (InterruptedException ignore) {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
// Close all connections, whether safe or not
|
||||
for (int i = 0; i < currConnections; i++) {
|
||||
try {
|
||||
connPool[i].close();
|
||||
} catch (SQLException e1) {
|
||||
log.debug("Cannot close connections on Destroy.");
|
||||
}
|
||||
}
|
||||
|
||||
if (useCount > 0) {
|
||||
//bt-test successful
|
||||
String msg = "Unsafe shutdown: Had to close " + useCount + " active DB connections after " + millis + "ms.";
|
||||
log.error(msg);
|
||||
// Throwing following Exception is essential because servlet authors
|
||||
// are likely to have their own error logging requirements.
|
||||
throw new SQLException(msg);
|
||||
}
|
||||
}//End destroy()
|
||||
|
||||
|
||||
/**
|
||||
* Less safe shutdown. Uses default timeout value.
|
||||
* This method simply calls the <code>destroy()</code> method
|
||||
* with a <code>millis</code>
|
||||
* value of 10000 (10 seconds) and ignores <code>SQLException</code>
|
||||
* thrown by that method.
|
||||
*
|
||||
* @see #destroy(int)
|
||||
*/
|
||||
public void destroy() {
|
||||
try {
|
||||
destroy(10000);
|
||||
} catch (SQLException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of connections in use.
|
||||
*/
|
||||
// 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() {
|
||||
int useCount = 0;
|
||||
synchronized (connStatus) {
|
||||
for (int i = 0; i < currConnections; i++) {
|
||||
if (connStatus[i] > 0) { // In use
|
||||
useCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return useCount;
|
||||
}//End getUseCount()
|
||||
|
||||
/**
|
||||
* Returns the number of connections in the dynamic pool.
|
||||
*/
|
||||
public int getSize() {
|
||||
return currConnections;
|
||||
}//End getSize()
|
||||
|
||||
}// End class
|
276
src/net/java/dev/simplepool/SimplePoolConnection.java
Normal file
276
src/net/java/dev/simplepool/SimplePoolConnection.java
Normal file
|
@ -0,0 +1,276 @@
|
|||
/**
|
||||
* $Source$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (c) 2004, Russell Beattie (http://www.russellbeattie.com/)
|
||||
* All rights 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 conditions are
|
||||
* met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions 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 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;
|
||||
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* SimplePoolConnection
|
||||
*
|
||||
* @author Russell Beattie
|
||||
* @author Erik C. Thauvin
|
||||
* @version $Revision$, $Date$
|
||||
* @since 1.0
|
||||
*/
|
||||
public class SimplePoolConnection implements Connection {
|
||||
|
||||
protected Connection conn;
|
||||
protected SimplePool broker;
|
||||
|
||||
public SimplePoolConnection(SimplePool broker) {
|
||||
this.broker = broker;
|
||||
conn = broker.getConnection();
|
||||
}
|
||||
|
||||
public void setAutoCommit(boolean arg0)
|
||||
throws SQLException {
|
||||
conn.setAutoCommit(arg0);
|
||||
}
|
||||
|
||||
public boolean getAutoCommit()
|
||||
throws SQLException {
|
||||
return conn.getAutoCommit();
|
||||
}
|
||||
|
||||
|
||||
public void setCatalog(String arg0)
|
||||
throws SQLException {
|
||||
conn.setCatalog(arg0);
|
||||
}
|
||||
|
||||
|
||||
public String getCatalog()
|
||||
throws SQLException {
|
||||
return conn.getCatalog();
|
||||
}
|
||||
|
||||
|
||||
public boolean isClosed()
|
||||
throws SQLException {
|
||||
return conn.isClosed();
|
||||
}
|
||||
|
||||
|
||||
public void setHoldability(int arg0)
|
||||
throws SQLException {
|
||||
conn.setHoldability(arg0);
|
||||
}
|
||||
|
||||
|
||||
public int getHoldability()
|
||||
throws SQLException {
|
||||
return conn.getHoldability();
|
||||
}
|
||||
|
||||
|
||||
public DatabaseMetaData getMetaData()
|
||||
throws SQLException {
|
||||
return conn.getMetaData();
|
||||
}
|
||||
|
||||
|
||||
public void setReadOnly(boolean arg0)
|
||||
throws SQLException {
|
||||
conn.setReadOnly(arg0);
|
||||
}
|
||||
|
||||
|
||||
public boolean isReadOnly()
|
||||
throws SQLException {
|
||||
return conn.isReadOnly();
|
||||
}
|
||||
|
||||
|
||||
public Savepoint setSavepoint()
|
||||
throws SQLException {
|
||||
return conn.setSavepoint();
|
||||
}
|
||||
|
||||
|
||||
public Savepoint setSavepoint(String arg0)
|
||||
throws SQLException {
|
||||
return conn.setSavepoint(arg0);
|
||||
}
|
||||
|
||||
|
||||
public void setTransactionIsolation(int arg0)
|
||||
throws SQLException {
|
||||
conn.setTransactionIsolation(arg0);
|
||||
}
|
||||
|
||||
|
||||
public int getTransactionIsolation()
|
||||
throws SQLException {
|
||||
return conn.getTransactionIsolation();
|
||||
}
|
||||
|
||||
|
||||
public void setTypeMap(Map arg0)
|
||||
throws SQLException {
|
||||
conn.setTypeMap(arg0);
|
||||
}
|
||||
|
||||
|
||||
public Map getTypeMap()
|
||||
throws SQLException {
|
||||
return conn.getTypeMap();
|
||||
}
|
||||
|
||||
|
||||
public SQLWarning getWarnings()
|
||||
throws SQLException {
|
||||
return conn.getWarnings();
|
||||
}
|
||||
|
||||
|
||||
public void clearWarnings()
|
||||
throws SQLException {
|
||||
conn.clearWarnings();
|
||||
}
|
||||
|
||||
|
||||
public void close()
|
||||
throws SQLException {
|
||||
broker.freeConnection(conn);
|
||||
}
|
||||
|
||||
|
||||
public void commit()
|
||||
throws SQLException {
|
||||
conn.commit();
|
||||
}
|
||||
|
||||
|
||||
public Statement createStatement()
|
||||
throws SQLException {
|
||||
return conn.createStatement();
|
||||
}
|
||||
|
||||
|
||||
public Statement createStatement(int arg0, int arg1)
|
||||
throws SQLException {
|
||||
return conn.createStatement(arg0, arg1);
|
||||
}
|
||||
|
||||
|
||||
public Statement createStatement(int arg0, int arg1, int arg2)
|
||||
throws SQLException {
|
||||
return conn.createStatement(arg0, arg1, arg2);
|
||||
}
|
||||
|
||||
|
||||
public String nativeSQL(String arg0)
|
||||
throws SQLException {
|
||||
return conn.nativeSQL(arg0);
|
||||
}
|
||||
|
||||
|
||||
public CallableStatement prepareCall(String arg0)
|
||||
throws SQLException {
|
||||
return conn.prepareCall(arg0);
|
||||
}
|
||||
|
||||
|
||||
public CallableStatement prepareCall(String arg0, int arg1, int arg2)
|
||||
throws SQLException {
|
||||
return conn.prepareCall(arg0, arg1, arg2);
|
||||
}
|
||||
|
||||
|
||||
public CallableStatement prepareCall(String arg0, int arg1, int arg2, int arg3)
|
||||
throws SQLException {
|
||||
return conn.prepareCall(arg0, arg1, arg2, arg3);
|
||||
}
|
||||
|
||||
|
||||
public PreparedStatement prepareStatement(String arg0)
|
||||
throws SQLException {
|
||||
return conn.prepareStatement(arg0);
|
||||
}
|
||||
|
||||
|
||||
public PreparedStatement prepareStatement(String arg0, int arg1)
|
||||
throws SQLException {
|
||||
return conn.prepareStatement(arg0);
|
||||
}
|
||||
|
||||
|
||||
public PreparedStatement prepareStatement(String arg0, int arg1, int arg2)
|
||||
throws SQLException {
|
||||
return conn.prepareStatement(arg0, arg1, arg2);
|
||||
}
|
||||
|
||||
|
||||
public PreparedStatement prepareStatement(String arg0, int arg1, int arg2, int arg3)
|
||||
throws SQLException {
|
||||
return conn.prepareStatement(arg0, arg1, arg2, arg3);
|
||||
}
|
||||
|
||||
|
||||
public PreparedStatement prepareStatement(String arg0, int[] arg1)
|
||||
throws SQLException {
|
||||
return conn.prepareStatement(arg0, arg1);
|
||||
}
|
||||
|
||||
|
||||
public PreparedStatement prepareStatement(String arg0, String[] arg1)
|
||||
throws SQLException {
|
||||
return conn.prepareStatement(arg0, arg1);
|
||||
}
|
||||
|
||||
|
||||
public void releaseSavepoint(Savepoint arg0)
|
||||
throws SQLException {
|
||||
conn.releaseSavepoint(arg0);
|
||||
}
|
||||
|
||||
|
||||
public void rollback()
|
||||
throws SQLException {
|
||||
conn.rollback();
|
||||
}
|
||||
|
||||
|
||||
public void rollback(Savepoint arg0)
|
||||
throws SQLException {
|
||||
conn.rollback(arg0);
|
||||
}
|
||||
}
|
189
src/net/java/dev/simplepool/SimplePoolDataSource.java
Normal file
189
src/net/java/dev/simplepool/SimplePoolDataSource.java
Normal file
|
@ -0,0 +1,189 @@
|
|||
/**
|
||||
* $Source$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (c) 2004, Russell Beattie (http://www.russellbeattie.com/)
|
||||
* All rights 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 conditions are
|
||||
* met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions 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 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;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.PrintWriter;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* SimplePoolDataSource
|
||||
*
|
||||
* @author Russell Beattie
|
||||
* @author Erik C. Thauvin
|
||||
* @version $Revision$, $Date$
|
||||
* @since 1.0
|
||||
*/
|
||||
public class SimplePoolDataSource implements DataSource {
|
||||
|
||||
protected PrintWriter logWriter = new PrintWriter(System.out);
|
||||
|
||||
protected SimplePool broker = null;
|
||||
|
||||
private String driver = "";
|
||||
private String jdbcUrl = "";
|
||||
private String user = "";
|
||||
private String password = "";
|
||||
private String minConns = "";
|
||||
private String maxConns = "";
|
||||
private String maxConnTime = "";
|
||||
private String maxCheckoutSeconds = "";
|
||||
|
||||
public SimplePoolDataSource() {
|
||||
;
|
||||
}
|
||||
|
||||
public void init() throws Exception {
|
||||
|
||||
broker = new SimplePool(driver, jdbcUrl, user, password,
|
||||
Integer.parseInt(minConns), Integer.parseInt(maxConns),
|
||||
Double.parseDouble(maxConnTime), Integer.parseInt(maxCheckoutSeconds));
|
||||
|
||||
}
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
|
||||
if (broker == null) {
|
||||
try {
|
||||
init();
|
||||
} catch (Exception e) {
|
||||
throw new SQLException("Error initializing Connection Broker.");
|
||||
}
|
||||
}
|
||||
|
||||
return new SimplePoolConnection(broker);
|
||||
|
||||
}
|
||||
|
||||
public Connection getConnection(String user, String password)
|
||||
throws SQLException {
|
||||
|
||||
throw new SQLException("Not supported in this DataSource.");
|
||||
|
||||
}
|
||||
|
||||
public void setLogWriter(PrintWriter output) throws SQLException {
|
||||
logWriter = output;
|
||||
}
|
||||
|
||||
public PrintWriter getLogWriter() {
|
||||
return logWriter;
|
||||
}
|
||||
|
||||
public void setLoginTimeout(int seconds) throws SQLException {
|
||||
}
|
||||
|
||||
public int getLoginTimeout() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
broker.destroy();
|
||||
broker = null;
|
||||
}
|
||||
|
||||
|
||||
// GET/SETs
|
||||
|
||||
public void setDriver(String val) {
|
||||
driver = val;
|
||||
}
|
||||
|
||||
public String getDriver() {
|
||||
return driver;
|
||||
}
|
||||
|
||||
public void setJdbcUrl(String val) {
|
||||
jdbcUrl = val;
|
||||
}
|
||||
|
||||
public String getJdbcUrl() {
|
||||
return jdbcUrl;
|
||||
}
|
||||
|
||||
public void setUser(String val) {
|
||||
user = val;
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setPassword(String val) {
|
||||
password = val;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setMinConns(String val) {
|
||||
minConns = val;
|
||||
}
|
||||
|
||||
public String getMinConns() {
|
||||
return minConns;
|
||||
}
|
||||
|
||||
public void setMaxConns(String val) {
|
||||
maxConns = val;
|
||||
}
|
||||
|
||||
public String getMaxConns() {
|
||||
return maxConns;
|
||||
}
|
||||
|
||||
public void setMaxConnTime(String val) {
|
||||
maxConnTime = val;
|
||||
}
|
||||
|
||||
public String getMaxConnTime() {
|
||||
return maxConnTime;
|
||||
}
|
||||
|
||||
public void setMaxCheckoutSeconds(String val) {
|
||||
maxCheckoutSeconds = val;
|
||||
}
|
||||
|
||||
public String getMaxCheckoutSeconds() {
|
||||
return maxCheckoutSeconds;
|
||||
}
|
||||
|
||||
}
|
126
src/net/java/dev/simplepool/SimplePoolDataSourceFactory.java
Normal file
126
src/net/java/dev/simplepool/SimplePoolDataSourceFactory.java
Normal file
|
@ -0,0 +1,126 @@
|
|||
/**
|
||||
* $Source$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (c) 2004, Russell Beattie (http://www.russellbeattie.com/)
|
||||
* All rights 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 conditions are
|
||||
* met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions 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 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;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.Name;
|
||||
import javax.naming.Reference;
|
||||
import javax.naming.spi.ObjectFactory;
|
||||
import java.util.Hashtable;
|
||||
|
||||
/**
|
||||
* SimplePoolDataSourceFactory
|
||||
*
|
||||
* @author Russell Beattie
|
||||
* @author Erik C. Thauvin
|
||||
* @version $Revision$, $Date$
|
||||
* @since 1.0
|
||||
*/
|
||||
public class SimplePoolDataSourceFactory implements ObjectFactory {
|
||||
|
||||
public Object getObjectInstance(Object obj, Name name, Context ctx, Hashtable env)
|
||||
throws Exception {
|
||||
|
||||
Reference ref = (Reference) obj;
|
||||
SimplePoolDataSource dataSource = new SimplePoolDataSource();
|
||||
|
||||
/*
|
||||
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>
|
||||
user: Database login name. e.g. 'Scott'<br>
|
||||
password: Database password. e.g. 'Tiger'<br>
|
||||
minConns: Minimum number of connections to start with.<br>
|
||||
maxConns: Maximum number of connections in dynamic pool.<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.
|
||||
*/
|
||||
|
||||
String driver = (String) ref.get("driver").getContent();
|
||||
|
||||
if (driver != null) {
|
||||
dataSource.setDriver(driver);
|
||||
}
|
||||
|
||||
String user = (String) ref.get("user").getContent();
|
||||
|
||||
if (user != null) {
|
||||
dataSource.setUser(user);
|
||||
}
|
||||
|
||||
String password = (String) ref.get("password").getContent();
|
||||
|
||||
if (password != null) {
|
||||
dataSource.setPassword(password);
|
||||
}
|
||||
|
||||
String jdbcUrl = (String) ref.get("jdbcUrl").getContent();
|
||||
|
||||
if (jdbcUrl != null) {
|
||||
dataSource.setJdbcUrl(jdbcUrl);
|
||||
}
|
||||
|
||||
String minConns = (String) ref.get("minConns").getContent();
|
||||
|
||||
if (minConns != null) {
|
||||
dataSource.setMinConns(minConns);
|
||||
}
|
||||
|
||||
String maxConns = (String) ref.get("maxConns").getContent();
|
||||
|
||||
if (maxConns != null) {
|
||||
dataSource.setMaxConns(maxConns);
|
||||
}
|
||||
|
||||
String maxConnTime = (String) ref.get("maxConnTime").getContent();
|
||||
|
||||
if (maxConnTime != null) {
|
||||
dataSource.setMaxConnTime(maxConnTime);
|
||||
}
|
||||
|
||||
String maxCheckoutSeconds = (String) ref.get("maxCheckoutSeconds").getContent();
|
||||
|
||||
if (maxCheckoutSeconds != null) {
|
||||
dataSource.setMaxCheckoutSeconds(maxCheckoutSeconds);
|
||||
}
|
||||
|
||||
dataSource.init();
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
}
|
116
src/net/java/dev/simplepool/SimplePoolServlet.java
Normal file
116
src/net/java/dev/simplepool/SimplePoolServlet.java
Normal file
|
@ -0,0 +1,116 @@
|
|||
/**
|
||||
* $Source$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (c) 2004, Russell Beattie (http://www.russellbeattie.com/)
|
||||
* All rights 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 conditions are
|
||||
* met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions 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 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;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
|
||||
|
||||
/**
|
||||
* SimplePoolServlet
|
||||
*
|
||||
* @author Russell Beattie
|
||||
* @author Erik C. Thauvin
|
||||
* @version $Revision$, $Date$
|
||||
* @since 1.0
|
||||
*/
|
||||
public class SimplePoolServlet extends HttpServlet {
|
||||
|
||||
private static final Log log = LogFactory.getLog(SimplePoolServlet.class);
|
||||
private static SimplePoolDataSource dataSource;
|
||||
|
||||
public void destroy() {
|
||||
if (dataSource != null) {
|
||||
dataSource.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void init(ServletConfig servletConfig)
|
||||
throws ServletException {
|
||||
log.info("Initializing " + servletConfig.getServletName() + " Servlet");
|
||||
|
||||
String varName = servletConfig.getInitParameter("varName");
|
||||
String driver = servletConfig.getInitParameter("driver");
|
||||
String jdbcUrl = servletConfig.getInitParameter("jdbcUrl");
|
||||
String user = servletConfig.getInitParameter("user");
|
||||
String password = servletConfig.getInitParameter("password");
|
||||
String minConns = servletConfig.getInitParameter("minConns");
|
||||
String maxConns = servletConfig.getInitParameter("maxConns");
|
||||
String maxConnTime = servletConfig.getInitParameter("maxConnTime");
|
||||
String maxCheckoutSeconds = servletConfig.getInitParameter("maxCheckoutSeconds");
|
||||
|
||||
if (isValid(varName) && isValid(driver) && isValid(jdbcUrl) && (user != null) && (password != null) &&
|
||||
isValid(minConns) && isValid(maxConns) && isValid(maxConnTime) && isValid(maxCheckoutSeconds)) {
|
||||
dataSource = new SimplePoolDataSource();
|
||||
|
||||
dataSource.setDriver(driver);
|
||||
dataSource.setJdbcUrl(jdbcUrl);
|
||||
dataSource.setUser(user);
|
||||
dataSource.setPassword(password);
|
||||
dataSource.setMinConns(minConns);
|
||||
dataSource.setMaxConns(maxConns);
|
||||
dataSource.setMaxConnTime(maxConnTime);
|
||||
dataSource.setMaxCheckoutSeconds(maxCheckoutSeconds);
|
||||
|
||||
try {
|
||||
dataSource.init();
|
||||
|
||||
servletConfig.getServletContext().setAttribute(varName, dataSource);
|
||||
} catch (Exception e) {
|
||||
log.error("An error occurred while starting the connection pool.", e);
|
||||
throw new ServletException("Error starting the connection pool.");
|
||||
}
|
||||
} else {
|
||||
log.error("One or more intialization parameter is invalid or missing.");
|
||||
throw new ServletException("Invalid or missing initialization parameter.");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValid(String s) {
|
||||
if ((s != null) && (s.length() > 0)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue