This is a heavily modified version of the connection pool from http://www.WebDevelopersJournal.com/columns/connection_pool.html, It was originally an inner class of DBConnectionPoolManager. Reports configuration errors via exceptions instead of returning null.
Copyright 2002 by Brad Cox: <bcox@virtualschool.edu>
Definition at line 24 of file DBPool.java.
Public Member Functions | |
| DBPool (String path) throws Config.Fault, Fault | |
| DBPool (File path) throws Config.Fault, Fault | |
| DBPool (Config config) throws Config.Fault, Fault | |
| DBPool (String url, String user, String password, String maxConnections, String[] driverList) throws Fault | |
| Connection | checkout () throws Fault |
| synchronized Connection | checkout (long timeout) throws Fault |
| synchronized void | checkin (Connection connection) |
| final String | toString () |
| final int | getMaxConnections () |
| final int | getNumberBusyConnections () |
| final int | getNumberPooledConnections () |
| synchronized void | releaseAllConnections () throws Fault |
Public Attributes | |
| int | numberBusyConnections |
Package Attributes | |
| boolean | isInitialized = false |
| final String | url |
| final String | password |
| final String | user |
| final int | maxConnections |
| final String[] | driverList |
| final Vector | pooledConnections = new Vector() |
|
|
Construct a Connection pool from pathname of JDBC properties file.
Definition at line 41 of file DBPool.java.
00042 {
00043 this(Config.loadFile(path));
00044 }
|
|
||||||||||||||||||||||||
|
Connection pool constructor. Notice that driverList installation is deferred until the first connection is created. This was done so that the constructor can be exception-free to simplify the client API and to minimize constructor/destructor overhead. DriverList problems will be reported when the first connection is created.
Definition at line 73 of file DBPool.java.
00080 {
00081 this.driverList = driverList;
00082 if (driverList == null)
00083 throw new Fault("driverList may not be null");
00084 if (driverList.length <= 0)
00085 throw new Fault("driverList length may not be zero");
00086
00087 this.url = url;
00088 if ( url == null)
00089 throw new Fault("url may not be null");
00090
00091 this.user = user;
00092 if (user == null)
00093 throw new Fault("user may not be null");
00094
00095 this.password = password;
00096 if (password == null)
00097 throw new Fault("password may not be null");
00098
00099 this.numberBusyConnections = 0;
00100 this.isInitialized = false;
00101
00102 if (maxConnections == null)
00103 throw new Fault("maxConnections may not be null");
00104 try
00105 {
00106 this.maxConnections = Integer.parseInt(maxConnections);
00107 }
00108 catch (NumberFormatException e1)
00109 {
00110 throw new Fault(maxConnections+" is not an integer");
00111 }
00112 }
|
|
|
Put the connection back in the pool for reuse. Notify other Threads that might be waiting for a connection.
Definition at line 166 of file DBPool.java. Referenced by edu.virtualschool.jwaa.dbms.DB.close().
00167 {
00168 if (connection != null) // paranoia
00169 {
00170 pooledConnections.add(connection);
00171 numberBusyConnections--;
00172 super.notifyAll();
00173 }
00174 }
|
|
|
Checks out a connection from the pool. If no free connection is available, a new connection is created unless the max number of connections has been reached. If a free connection has been closed by the database, it's removed from the pool and this method is called again recursively. If no connection is available and the max number has been reached, this method waits the specified time for one to be checked in.
Definition at line 131 of file DBPool.java.
00132 {
00133 long startTime = System.currentTimeMillis();
00134 Connection connection = null;
00135 while((connection = getConnection()) == null)
00136 {
00137 try
00138 {
00139 super.wait(timeout);
00140 }
00141 catch (InterruptedException e)
00142 {
00143 e.printStackTrace();
00144 }
00145 if ((System.currentTimeMillis() - startTime) >= timeout)
00146 throw new Fault("Connection pool timeout: " + timeout, null);
00147 }
00148 numberBusyConnections++;
00149 try
00150 {
00151 connection.setAutoCommit(false);
00152 connection.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
00153 }
00154 catch (SQLException e)
00155 {
00156 throw new DBPool.Fault
00157 ("Fault setting up connection #"+numberBusyConnections, e);
00158 }
00159 return connection;
00160 }
|
|
|
Checks out a connection from the pool with default timout of 10 seconds. Definition at line 117 of file DBPool.java. Referenced by edu.virtualschool.jwaa.dbms.DB.DB().
00117 { return checkout(10*1000); }
|
|
|
Returns the maximum size of the pool as set by the constructor.
Definition at line 180 of file DBPool.java.
00180 { return maxConnections; }
|
|
|
Return numberBusyConnections. This increments each get() and decrements for each returnConnection().
Definition at line 186 of file DBPool.java.
00186 { return numberBusyConnections; }
|
|
|
Returns the size of the connection pool, e.g. the number of connections returned by returnConnection().
Definition at line 192 of file DBPool.java.
00192 { return pooledConnections.size(); }
|
|
|
Release all pooled connections. Definition at line 275 of file DBPool.java.
00276 {
00277 for (Enumeration e = pooledConnections.elements(); e.hasMoreElements(); )
00278 {
00279 Connection c = (Connection) e.nextElement();
00280 try { c.close(); }
00281 catch (SQLException ex)
00282 { throw new Fault(ex, ex); }
00283 }
00284 pooledConnections.removeAllElements();
00285 }
|