xmlBlaster 2.2.0 API

org.xmlBlaster.protocol.jdbc
Class NamedConnectionPool

java.lang.Object
  extended by org.xmlBlaster.protocol.jdbc.NamedConnectionPool

public class NamedConnectionPool
extends java.lang.Object

This is a specialized JDBC connection pool for xmlBlaster.

It allows accessing any number of different databases with arbitrary login users.
Every database user is separately pooled for maximum performance.
Every DB request needs to pass the DB-url, login name and password, since the clients are not permanently connected.
Unused connection pools are freed after some time.

The timeout parameters and pool size is adjustable.

The connections are established on demand (lazy allocation). Pre-allocation is currently not implemented. The first SQL request (for example with Oracle) consumes about 1 second to establish the connection, the following requests get this connection from the pool, which is below 1 millisecond. If more than one SQL requests are done simultaneously, the pool increases the number of parallel connections.

Load the drivers before using this pool, e.g.

    java -Djdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver ...
 
or in xmlBlaster.properties, e.g.
    JdbcDriver.drivers=oracle.jdbc.driver.OracleDriver,org.gjt.mm.mysql.Driver:postgresql.Driver,de.sag.jdbc.adabasd.ADriver:sun.jdbc.odbc.JdbcOdbcDriver:com.sybase.jdbc2.jdbc.SybDriver
 
You use reserve() to get a connection and need to call release() after using it, note that the connection parameters are optional:
    String dbStmt = "select * from user_table";
    java.sql.Connection con = namedPool.reserve(dbUrl, dbUser, dbPasswd, 60*60*1000L, 100, 10*60*1000L);
    java.sql.Statement stmt = null;
    java.sql.ResultSet rs = null;
    try {
       stmt = con.createStatement();
       rs = stmt.executeQuery(dbStmt);
    } finally {
       if (rs!=null) rs.close();
       if (stmt!=null) stmt.close();
       if (con!=null) namedPool.release(dbUrl, user, pw, con);
    }
 

Since:
xmlBlaster 0.78
See Also:
PoolManager, ResourceWrapper

Nested Class Summary
private  class NamedConnectionPool.UnnamedConnectionPool
          Inner class, every user of the Named pool has its own connection pool.
 
Field Summary
private  Global glob
           
private static java.util.logging.Logger log
           
private static java.lang.String ME
           
private  java.lang.Object meetingPoint
           
private  java.util.Hashtable namedPools
           
 
Constructor Summary
NamedConnectionPool(Global glob)
           
 
Method Summary
(package private)  void destroy()
          Destroy the complete named pool of all users.
(package private)  void destroy(java.lang.String dbUrl, java.lang.String dbUser, java.lang.String dbPasswd)
          Destroy the JDBC connection pool from a specific user.
(package private)  void eraseConnection(java.lang.String dbUrl, java.lang.String dbUser, java.lang.String dbPasswd, java.sql.Connection con)
          Use this method to destroy the given JDBC connection.
private  java.lang.String getKey(java.lang.String dbUrl, java.lang.String dbUser, java.lang.String dbPasswd)
           
private  NamedConnectionPool.UnnamedConnectionPool getPool(java.lang.String dbUrl, java.lang.String dbUser, java.lang.String dbPasswd)
           
static void main(java.lang.String[] args)
          For testing only.
(package private)  void release(java.lang.String dbUrl, java.lang.String dbUser, java.lang.String dbPasswd, java.sql.Connection con)
          Use this method to release a JDBC connection.
(package private)  java.sql.Connection reserve(java.lang.String dbUrl, java.lang.String dbUser, java.lang.String dbPasswd)
          Use this method to get a JDBC connection.
(package private)  java.sql.Connection reserve(java.lang.String dbUrl, java.lang.String dbUser, java.lang.String dbPasswd, long eraseUnusedPoolTimeout, int maxInstances, long idleToErase)
          Use this method to get a JDBC connection.
 java.lang.String toXml()
          Dump state of this object into a XML ASCII string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ME

private static final java.lang.String ME
See Also:
Constant Field Values

glob

private Global glob

log

private static java.util.logging.Logger log

namedPools

private java.util.Hashtable namedPools

meetingPoint

private final java.lang.Object meetingPoint
Constructor Detail

NamedConnectionPool

NamedConnectionPool(Global glob)
Method Detail

reserve

java.sql.Connection reserve(java.lang.String dbUrl,
                            java.lang.String dbUser,
                            java.lang.String dbPasswd)
                      throws XmlBlasterException
Use this method to get a JDBC connection.
The pooling properties are set to default values.

Parameters:
dbUrl - For example "jdbc:oracle:thin:@localhost:1521:mydb
dbUser - The database user
dbPasswd - The database password
Throws:
XmlBlasterException

reserve

java.sql.Connection reserve(java.lang.String dbUrl,
                            java.lang.String dbUser,
                            java.lang.String dbPasswd,
                            long eraseUnusedPoolTimeout,
                            int maxInstances,
                            long idleToErase)
                      throws XmlBlasterException
Use this method to get a JDBC connection.
Usually only the first time for a user, to specify all parameters.

Parameters:
dbUrl - For example "jdbc:oracle:thin:@localhost:1521:mydb
eraseUnusedPoolTimeout - Remove pool of a user if not in use, in ms 0 switches it off, -1 uses default setting 1 hour (from xmlBlaster.properties)
maxInstances - Default is max. 20 connections (from xmlBlaster.properties)
idleToErase - in msec 0 switches it off, -1 uses default setting 10 min. (from xmlBlaster.properties)
Throws:
XmlBlasterException

release

void release(java.lang.String dbUrl,
             java.lang.String dbUser,
             java.lang.String dbPasswd,
             java.sql.Connection con)
       throws XmlBlasterException
Use this method to release a JDBC connection.

Throws:
XmlBlasterException

eraseConnection

void eraseConnection(java.lang.String dbUrl,
                     java.lang.String dbUser,
                     java.lang.String dbPasswd,
                     java.sql.Connection con)
               throws XmlBlasterException
Use this method to destroy the given JDBC connection.

Throws:
XmlBlasterException

destroy

void destroy(java.lang.String dbUrl,
             java.lang.String dbUser,
             java.lang.String dbPasswd)
       throws XmlBlasterException
Destroy the JDBC connection pool from a specific user. The driver remains.

Parameters:
The - UnnamedConnectionPool object
Throws:
XmlBlasterException

destroy

void destroy()
Destroy the complete named pool of all users. This object is still valid for further use.


getKey

private java.lang.String getKey(java.lang.String dbUrl,
                                java.lang.String dbUser,
                                java.lang.String dbPasswd)
Returns:
instanceId ^/, e.g. "jdbc:oracle:thin:@localhost:1521:mydb^jack/secret"

getPool

private NamedConnectionPool.UnnamedConnectionPool getPool(java.lang.String dbUrl,
                                                          java.lang.String dbUser,
                                                          java.lang.String dbPasswd)

toXml

public final java.lang.String toXml()
Dump state of this object into a XML ASCII string.


main

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

Invoke: java org.xmlBlaster.protocol.jdbc.NamedConnectionPool -logging FINE


xmlBlaster 2.2.0 API

Copyright © 1999-2014 The xmlBlaster.org contributers.