@@ -27,6 +27,7 @@ public class Database implements Cloneable {
2727 private ConnectionPoolDataSource ConnectionPoolDataSource ;
2828 private static final Class <?>[] stringType = { String .class };
2929 private static final Class <?>[] integerType = { Integer .TYPE };
30+ private ConnectionPool connectionPool ;
3031
3132
3233 //**************************************************************************
@@ -410,16 +411,63 @@ else if (vendor.equals("Derby") || vendor.equals("SQLite")){
410411 //**************************************************************************
411412 //** getConnection
412413 //**************************************************************************
413- /** Used to open a connection to the database. Note the the connection will
414- * need to be closed afterwards.
414+ /** Used to open a connection to the database. If a connection pool has been
415+ * initialized (initConnectionPool), then an open connection is returned
416+ * from the pool. Otherwise, a new connection is created. In either case,
417+ * the connection must be closed when you are finished with it.
415418 */
416419 public Connection getConnection () throws SQLException {
417- Connection connection = new Connection ();
418- connection .open (this );
419- return connection ;
420+ if (connectionPool ==null ){
421+ Connection connection = new Connection ();
422+ connection .open (this );
423+ return connection ;
424+ }
425+ else {
426+ return connectionPool .getConnection ();
427+ }
420428 }
421429
422430
431+ //**************************************************************************
432+ //** initConnectionPool
433+ //**************************************************************************
434+ /** Used to initialize a connection pool. Subsequent called to the
435+ * getConnection() method will return connections from the pool.
436+ */
437+ public void initConnectionPool (int maxConnections ) throws SQLException {
438+ if (connectionPool !=null ) return ;
439+ connectionPool = new ConnectionPool (this , maxConnections );
440+
441+ //Create Shutdown Hook to clean up the connection pool on exit
442+ Runtime .getRuntime ().addShutdownHook (new Thread () {
443+ public void run () {
444+ if (connectionPool !=null ){
445+ System .out .println ("Shutting down..." );
446+ try {
447+ connectionPool .close ();
448+ }
449+ catch (Exception e ){
450+ e .printStackTrace ();
451+ }
452+ }
453+ }
454+ });
455+ }
456+
457+
458+ //**************************************************************************
459+ //** terminateConnectionPool
460+ //**************************************************************************
461+ /** Used to terminate the connection pool, closing all active connections.
462+ */
463+ public void terminateConnectionPool () throws SQLException {
464+ if (connectionPool !=null ){
465+ connectionPool .close ();
466+ connectionPool = null ;
467+ }
468+ }
469+
470+
423471 //**************************************************************************
424472 //** setConnectionPoolDataSource
425473 //**************************************************************************
0 commit comments