xmlBlaster 2.2.0 API

org.xmlBlaster.contrib.db
Class DbPool

java.lang.Object
  extended by org.xmlBlaster.contrib.db.DbPool
All Implemented Interfaces:
I_DbPool, I_ContribPlugin, I_PoolManager

public class DbPool
extends java.lang.Object
implements I_DbPool, I_PoolManager

Simple implementation of the database JDBC connection pool.

Following configuration paramaters are available:

 db.url=jdbc:oracle:thin:@localhost:1521:orcl
 db.user=system
 db.password=manager
 db.maxInstances=10
 db.busyToIdleTimeout=0             // How long may a query last [millis]
 db.idleToEraseTimeout=10*60*1000L  // How long does an unused connection survive (10 min)
 db.maxResourceExhaustRetries=5
 db.resourceExhaustSleepGap=1000    // [millis]
 

Author:
Marcel Ruff

Field Summary
private  I_DbCreateInterceptor createInterceptor
           
private  java.lang.String dbPasswd
           
private  java.lang.String dbUrl
           
private  java.lang.String dbUser
           
private  I_Info info
           
private  int initCount
           
private static java.util.logging.Logger log
           
private  int maxResourceExhaustRetries
          If the pool is exhausted, we poll the given times
private  java.lang.Object meetingPoint
           
private  PoolManager poolManager
           
private  long resourceExhaustSleepGap
          If the pool is exhausted, we poll every given millis
Please note that the current request thread will block for maxResourceExhaustRetries*resourceExhaustSleepGap millis.
 
Constructor Summary
DbPool()
          Default constructor, you need to call init(info) thereafter.
 
Method Summary
 void busyToIdle(java.lang.Object resource)
          This callback does nothing (enforced by interface I_PoolManager
private  void destroy()
          Cleanup and destroy everything.
 void erase(java.sql.Connection con)
          Destroy the JDBC connection
 I_Info getInfo()
          may be empty and just contains db.* properties
 java.util.Set getUsedPropertyKeys()
          Gets all property keys which may be used by this object.
 java.lang.String getUser()
           
 void idleToBusy(java.lang.Object resource)
          This callback does nothing (enforced by interface I_PoolManager)
 void init(I_Info info)
          Needs to be called after construction.
private  void initDrivers()
          Load the JDBC drivers given in environment.
static void main(java.lang.String[] args)
           
 void release(java.sql.Connection con)
          Return the JDBC connection to the pool.
 java.sql.Connection reserve()
          Access a JDBC connection.
 java.sql.Connection select(java.sql.Connection connection, java.lang.String command, boolean autoCommit, I_ResultCb cb)
          To have full control.
 java.sql.Connection select(java.sql.Connection connection, java.lang.String command, I_ResultCb cb)
          Convenience method to execute a SELECT SQL command.
 void select(java.lang.String command, I_ResultCb cb)
          Convenience method to execute a SELECT SQL command in auto commit mode.
 void shutdown()
          Close all open connections and destroy the pool.
 java.lang.Object toCreate(java.lang.String instanceId)
          Create a new JDBC connection, the driver must be registered already.
 void toErased(java.lang.Object resource)
          Destroy the JDBC connection.
 int update(java.sql.Connection conn, java.lang.String command)
           
 int update(java.lang.String command)
          Convenience method to execute a INSERT/UPDATE/CREATE or DROP SQL command in auto commit mode.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static java.util.logging.Logger log

info

private I_Info info

poolManager

private PoolManager poolManager

dbUrl

private java.lang.String dbUrl

dbUser

private java.lang.String dbUser

dbPasswd

private java.lang.String dbPasswd

maxResourceExhaustRetries

private int maxResourceExhaustRetries
If the pool is exhausted, we poll the given times


resourceExhaustSleepGap

private long resourceExhaustSleepGap
If the pool is exhausted, we poll every given millis
Please note that the current request thread will block for maxResourceExhaustRetries*resourceExhaustSleepGap millis.


meetingPoint

private final java.lang.Object meetingPoint

initCount

private int initCount

createInterceptor

private I_DbCreateInterceptor createInterceptor
Constructor Detail

DbPool

public DbPool()
Default constructor, you need to call init(info) thereafter.

Method Detail

getUsedPropertyKeys

public java.util.Set getUsedPropertyKeys()
Description copied from interface: I_ContribPlugin
Gets all property keys which may be used by this object.

Specified by:
getUsedPropertyKeys in interface I_ContribPlugin
Returns:
See Also:
I_ContribPlugin.getUsedPropertyKeys()

init

public void init(I_Info info)
Description copied from interface: I_DbPool
Needs to be called after construction.

Specified by:
init in interface I_DbPool
Specified by:
init in interface I_ContribPlugin
Parameters:
info - The configuration
See Also:
org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#init(I_Info)

getUser

public java.lang.String getUser()
Specified by:
getUser in interface I_DbPool
Returns:
the name of the user

initDrivers

private void initDrivers()
Load the JDBC drivers given in environment.

Default is JdbcDriver.drivers=sun.jdbc.odbc.JdbcOdbcDriver


reserve

public java.sql.Connection reserve()
                            throws java.lang.Exception
Description copied from interface: I_DbPool
Access a JDBC connection.

Specified by:
reserve in interface I_DbPool
Returns:
The JDBC connection
Throws:
java.lang.Exception - of type XmlBlasterException or IllegalArgumentException
See Also:
org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#reserve()

release

public void release(java.sql.Connection con)
             throws java.lang.Exception
Description copied from interface: I_DbPool
Return the JDBC connection to the pool.

Specified by:
release in interface I_DbPool
Parameters:
con - The JDBC connection
Throws:
java.lang.Exception - of type XmlBlasterException or IllegalArgumentException
See Also:
org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#release(java.sql.Connection)

erase

public void erase(java.sql.Connection con)
           throws java.lang.IllegalArgumentException
Description copied from interface: I_DbPool
Destroy the JDBC connection

Specified by:
erase in interface I_DbPool
Parameters:
con - The JDBC connection
Throws:
java.lang.IllegalArgumentException
See Also:
org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#erase(java.sql.Connection)

destroy

private void destroy()
Cleanup and destroy everything.


idleToBusy

public void idleToBusy(java.lang.Object resource)
This callback does nothing (enforced by interface I_PoolManager)

Specified by:
idleToBusy in interface I_PoolManager
Parameters:
resource - The Connection object
See Also:
I_PoolManager.idleToBusy(Object)

busyToIdle

public void busyToIdle(java.lang.Object resource)
This callback does nothing (enforced by interface I_PoolManager

Specified by:
busyToIdle in interface I_PoolManager
Parameters:
resource - The Connection object
See Also:
I_PoolManager.busyToIdle(Object)

toCreate

public java.lang.Object toCreate(java.lang.String instanceId)
Create a new JDBC connection, the driver must be registered already.

Specified by:
toCreate in interface I_PoolManager
Parameters:
instanceId - A unique identifier
Returns:
The JDBC connection

toErased

public void toErased(java.lang.Object resource)
Destroy the JDBC connection. The driver remains.

Specified by:
toErased in interface I_PoolManager
Parameters:
resource - The Connection object

update

public int update(java.lang.String command)
           throws java.lang.Exception
Description copied from interface: I_DbPool
Convenience method to execute a INSERT/UPDATE/CREATE or DROP SQL command in auto commit mode.

Specified by:
update in interface I_DbPool
Parameters:
command - for example INSERT INTO TEST_POLL VALUES ('1', 'EDDI')
Returns:
Number of touched entries
Throws:
java.lang.Exception - Typically a SQLException
See Also:
org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#update(String)

update

public int update(java.sql.Connection conn,
                  java.lang.String command)
           throws java.lang.Exception
Specified by:
update in interface I_DbPool
Throws:
java.lang.Exception
See Also:
org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#update(String)

select

public void select(java.lang.String command,
                   I_ResultCb cb)
            throws java.lang.Exception
Description copied from interface: I_DbPool
Convenience method to execute a SELECT SQL command in auto commit mode.

Specified by:
select in interface I_DbPool
Parameters:
command - for example SELECT * FROM TEST_POLL
cb - The callback handle for the retrieved ResultSet
Throws:
java.lang.Exception - Typically a SQLException
See Also:
org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#select(String, I_ResultCb)

select

public java.sql.Connection select(java.sql.Connection connection,
                                  java.lang.String command,
                                  I_ResultCb cb)
                           throws java.lang.Exception
Description copied from interface: I_DbPool
Convenience method to execute a SELECT SQL command.

On first call pass connection with null and a valid connection with an open transaction is returned by this method. You can now call this method passing the connection multiple times in the same transaction. When you are done you need to close the connection and put it back into the pool.

Example:
      Connection conn = null;

      try {
         conn = this.dbPool.select(conn, "SELECT A FROM B", new I_ResultCb() {
            public void result(ResultSet rs) throws Exception {
               // Processing result set
            }
         });

         conn = this.dbPool.select(conn, "SELECT B FROM  C", new I_ResultCb() {
            public void result(ResultSet rs) throws Exception {
               // Processing result set
            }
         });
      }
      finally {
         if (conn != null) {
            conn.commit();
            this.dbPool.release(conn);
         }
      }
 

Specified by:
select in interface I_DbPool
Parameters:
connection - If null a connection is created
command - for example SELECT * FROM TEST_POLL
cb - The callback handle for the retrieved ResultSet
Returns:
If not null you need to put the connection back to the pool yourself
Throws:
java.lang.Exception - Typically a SQLException
See Also:
org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#select(java.sql.Connection, String, I_ResultCb)

select

public java.sql.Connection select(java.sql.Connection connection,
                                  java.lang.String command,
                                  boolean autoCommit,
                                  I_ResultCb cb)
                           throws java.lang.Exception
Description copied from interface: I_DbPool
To have full control.

Specified by:
select in interface I_DbPool
Parameters:
connection - If null a connection is created
command - for example SELECT * FROM TEST_POLL
autoCommit - if true force auto commit and cleanup the connection, in this case we return null
cb - The callback handle for the retrieved ResultSet
Returns:
If not null you need to put the connection back to the pool yourself
Throws:
java.lang.Exception - Typically a SQLException
See Also:
org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#select(java.sql.Connection, String, I_ResultCb, boolean)

shutdown

public void shutdown()
Description copied from interface: I_DbPool
Close all open connections and destroy the pool. All resources are released.

Specified by:
shutdown in interface I_DbPool
Specified by:
shutdown in interface I_ContribPlugin
See Also:
org.xmlBlaster.contrib.dbwatcher.db.I_DbPool#shutdown

getInfo

public I_Info getInfo()
may be empty and just contains db.* properties

Specified by:
getInfo in interface I_DbPool
Returns:

main

public static void main(java.lang.String[] args)

xmlBlaster 2.2.0 API

Copyright © 1999-2014 The xmlBlaster.org contributers.