xmlBlaster 2.2.0 API

org.xmlBlaster.util.queue.jdbc
Class JdbcConnectionPool

java.lang.Object
  extended by org.xmlBlaster.util.queue.jdbc.JdbcConnectionPool
All Implemented Interfaces:
I_Timeout, I_StorageProblemNotifier

public class JdbcConnectionPool
extends java.lang.Object
implements I_Timeout, I_StorageProblemNotifier

A Pool of connections to the database to be used for a persistent queue. To keep genericity, queries and update strings are read from properties.

See Also:
The queue.jdbc.commontable requirement

Field Summary
private  int capacity
          the initial capacity of this pool.
private  boolean cascadeDeleteSupported
           
private  java.lang.String colNamePrefix
           
private  java.lang.String configurationIdentifier
           
private  long connectionBusyTimeout
           
private  java.util.concurrent.LinkedBlockingQueue<java.sql.Connection> connections
           
private  boolean dbAdmin
           
private  boolean debug
           
private  boolean enableBatchMode
           
private static boolean firstConnectError
           
private  int forceIsolationLevel
           
private  Global glob
           
private  boolean initialized
           
private  boolean isShutdown
           
private static java.util.logging.Logger log
           
private  int managerCount
           
private  java.util.Hashtable<java.lang.String,java.lang.String> mapping
           
private  int maxWaitingThreads
           
private static java.lang.String ME
           
private  int MIN_POOL_SIZE
           
private  boolean nestedBracketsSupported
           
private  java.lang.String password
           
private  java.util.Properties pluginProp
           
private  int queryTimeout
          Sets the number of seconds the driver will wait for a Statement object to execute to the given number of seconds.
private  long reconnectionTimeout
           
private  int status
          will be set when a connecton is broken
private  I_StorageProblemListener storageProblemListener
           
private  int tableAllocationIncrement
           
private  java.lang.String tableNamePrefix
           
private  java.lang.String url
           
private  java.lang.String user
           
private  int waitingCalls
           
private  boolean waitingForReentrantConnections
           
 
Constructor Summary
JdbcConnectionPool()
          The default constructor currently does nothing.
 
Method Summary
private  void addConnectionToPool(boolean doLog)
           
private  void connect(boolean disconnectFirst, boolean doLog)
          Connects to the DB (so many connections as configured)
 java.sql.Connection createNewConnection(boolean doLog)
          Independend of pool.
 void discardConnection(java.sql.Connection conn)
          Discards the passed connection from the pool and instead adds a new fresh connection to the pool.
 void disconnect()
          Closes all connections to the Database.
private  void disconnect(long waitTime, boolean silent)
           
 void dumpMetaData()
           
 void finalize()
           
private  java.sql.Connection get(long delay)
          returns null if no connection available
 java.lang.String getColNamePrefix()
           
 java.sql.Connection getConnection()
          Returns a free connection.
 Global getGlobal()
          Returns the global object associated with this pool.
static java.lang.String getIsolationLevel(java.sql.Connection conn)
           
 java.util.Hashtable<java.lang.String,java.lang.String> getMapping()
          The mapping is taken out of the xmlBlaster.properties file.
 java.util.Properties getPluginProperties()
          returns the plugin properties, i.e.
 int getProp(java.lang.String key, int def)
           
 java.lang.String getProp(java.lang.String key, java.lang.String def)
           
 int getQueryTimeout()
           
 int getStatus()
          returns true if the connection is temporarly lost (and the pool is polling for new connections)
 int getTableAllocationIncrement()
           
 java.lang.String getTableNamePrefix()
           
 java.lang.String getUrl()
           
 java.lang.String getUserName()
           
 void initialize(Global glob, java.util.Properties pluginProperties)
          Is called after the instance is created.
 boolean isBatchModeEnabled()
          The batch mode means that insertions in the database are made in batch mode, i.e.
 boolean isCascadeDeleteSuppported()
           
 boolean isDbAdmin()
           
 boolean isInitialized()
          returns true if the pool already is initialized, false otherwise.
private  boolean isInPool(java.sql.Connection conn)
           
 boolean isNestedBracketsSuppported()
           
static java.lang.String isolationToString(int isolation)
           
static void main(java.lang.String[] args)
           
private  java.util.Hashtable<java.lang.String,java.lang.String> parseMapping(Property prop)
          This method is used in the init method
private  boolean put(java.sql.Connection conn)
           
 void registerManager(java.lang.Object manager)
           
 boolean registerStorageProblemListener(I_StorageProblemListener storageProblemListener)
          Sets the connection listener.
private  void releaseConnection(java.sql.Connection conn)
          Used to give back a connection to the pool.
 void releaseConnection(java.sql.Connection conn, boolean success)
          Used to give back a connection to the pool.
 void setDatabaseLost()
          informs this pool that the connection to the DB has been lost
private  void setInPool(java.sql.Connection conn, boolean inPool)
           
 void shutdown()
           
 void timeout(java.lang.Object userData)
          Invoked by the timer when a check for reconnection is wanted again
 void unregisterManager(java.lang.Object manager)
           
 boolean unRegisterStorageProblemListener(I_StorageProblemListener storageProblemListener)
          Unregisters the storageProblemListener.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ME

private static java.lang.String ME

log

private static java.util.logging.Logger log

glob

private Global glob

connections

private java.util.concurrent.LinkedBlockingQueue<java.sql.Connection> connections

capacity

private int capacity
the initial capacity of this pool.


waitingCalls

private int waitingCalls

connectionBusyTimeout

private long connectionBusyTimeout

maxWaitingThreads

private int maxWaitingThreads

mapping

private java.util.Hashtable<java.lang.String,java.lang.String> mapping

initialized

private boolean initialized

tableNamePrefix

private java.lang.String tableNamePrefix

colNamePrefix

private java.lang.String colNamePrefix

tableAllocationIncrement

private int tableAllocationIncrement

status

private int status
will be set when a connecton is broken


waitingForReentrantConnections

private boolean waitingForReentrantConnections

url

private java.lang.String url

user

private java.lang.String user

password

private java.lang.String password

reconnectionTimeout

private long reconnectionTimeout

storageProblemListener

private I_StorageProblemListener storageProblemListener

firstConnectError

private static boolean firstConnectError

pluginProp

private java.util.Properties pluginProp

dbAdmin

private boolean dbAdmin

queryTimeout

private int queryTimeout
Sets the number of seconds the driver will wait for a Statement object to execute to the given number of seconds. If the limit is exceeded, an SQLException is thrown.


managerCount

private int managerCount

isShutdown

private boolean isShutdown

enableBatchMode

private boolean enableBatchMode

configurationIdentifier

private java.lang.String configurationIdentifier

cascadeDeleteSupported

private boolean cascadeDeleteSupported

nestedBracketsSupported

private boolean nestedBracketsSupported

forceIsolationLevel

private int forceIsolationLevel

debug

private boolean debug

MIN_POOL_SIZE

private final int MIN_POOL_SIZE
See Also:
Constant Field Values
Constructor Detail

JdbcConnectionPool

public JdbcConnectionPool()
The default constructor currently does nothing. Initialization is done in the initialize() method.

Method Detail

getPluginProperties

public java.util.Properties getPluginProperties()
returns the plugin properties, i.e. the specific properties passed to the jdbc queue plugin. These are commonly used by the jdbc manager.


timeout

public void timeout(java.lang.Object userData)
Invoked by the timer when a check for reconnection is wanted again

Specified by:
timeout in interface I_Timeout
Parameters:
userData - You get bounced back your userData which you passed with Timeout.addTimeoutListener()
See Also:
I_Timeout.timeout(Object)

registerStorageProblemListener

public boolean registerStorageProblemListener(I_StorageProblemListener storageProblemListener)
Sets the connection listener. Only one is allowed at a time. So if there is already a connection listener, it will be overwritten (and the old one will not get anyu notification anymore).

Specified by:
registerStorageProblemListener in interface I_StorageProblemNotifier

unRegisterStorageProblemListener

public boolean unRegisterStorageProblemListener(I_StorageProblemListener storageProblemListener)
Unregisters the storageProblemListener. If no one has been defined, or if the one you want to unregister is different from the one you have registered, nothing is done and 'false' is returned.

Specified by:
unRegisterStorageProblemListener in interface I_StorageProblemNotifier

get

private java.sql.Connection get(long delay)
                         throws XmlBlasterException
returns null if no connection available

Throws:
XmlBlasterException

put

private boolean put(java.sql.Connection conn)
             throws XmlBlasterException
Throws:
XmlBlasterException

getGlobal

public Global getGlobal()
Returns the global object associated with this pool.


isInitialized

public boolean isInitialized()
returns true if the pool already is initialized, false otherwise.


discardConnection

public void discardConnection(java.sql.Connection conn)
                       throws XmlBlasterException
Discards the passed connection from the pool and instead adds a new fresh connection to the pool. This method is to be invoked when an exception occured and it can be supposed that the connection is not good anymore.

Parameters:
conn -
Throws:
XmlBlasterException

isolationToString

public static java.lang.String isolationToString(int isolation)

getIsolationLevel

public static java.lang.String getIsolationLevel(java.sql.Connection conn)

setInPool

private final void setInPool(java.sql.Connection conn,
                             boolean inPool)

isInPool

private final boolean isInPool(java.sql.Connection conn)

createNewConnection

public java.sql.Connection createNewConnection(boolean doLog)
                                        throws java.sql.SQLException
Independend of pool.

Parameters:
doLog -
Returns:
Throws:
java.sql.SQLException

addConnectionToPool

private void addConnectionToPool(boolean doLog)
                          throws java.sql.SQLException
Throws:
java.sql.SQLException

connect

private void connect(boolean disconnectFirst,
                     boolean doLog)
              throws java.sql.SQLException
Connects to the DB (so many connections as configured)

Parameters:
disconnectFirst - if 'true' then all connections to the db are closed before reconnecting.
Throws:
java.sql.SQLException

getProp

public java.lang.String getProp(java.lang.String key,
                                java.lang.String def)

getProp

public int getProp(java.lang.String key,
                   int def)

initialize

public void initialize(Global glob,
                       java.util.Properties pluginProperties)
                throws java.lang.ClassNotFoundException,
                       java.sql.SQLException,
                       XmlBlasterException
Is called after the instance is created. It reads the needed properties, sets the driver and then connects to the database as many times as specified in the properties.

Note that prefix is the string which identifies a certain database. It is called prefix because it is the prefix in the xmlBlaster.properties for the properties bound to the database.
A pool instance is created for every such database. For example if you want the history queue (the queue which stores the published messages) on a different database than the callback queues, then you could define different databases.

  • prefix.driverName DEFAULTS TO "org.postgresql.Driver"
  • prefix.connectionPoolSize "DEFAULTS TO 2"
  • prefix.url DEFAULTS TO "jdbc:postgresql://localhost/test"
  • prefix.user DEFAULTS TO "postgres"
  • prefix.password DEFAULTS TO ""
  • Throws:
    java.lang.ClassNotFoundException
    java.sql.SQLException
    XmlBlasterException

    getTableNamePrefix

    public java.lang.String getTableNamePrefix()
    Returns:
    the prefix for the name of the tables to associate to the queues

    getUserName

    public java.lang.String getUserName()

    getColNamePrefix

    public java.lang.String getColNamePrefix()
    Returns:
    the prefix for the name of the columns in each DB table

    getTableAllocationIncrement

    public int getTableAllocationIncrement()
    Returns:
    the number of tables to add each time no free tables are available when creating a new queue.

    parseMapping

    private java.util.Hashtable<java.lang.String,java.lang.String> parseMapping(Property prop)
                                                                         throws XmlBlasterException,
                                                                                java.sql.SQLException
    This method is used in the init method

    Throws:
    XmlBlasterException
    java.sql.SQLException

    getMapping

    public java.util.Hashtable<java.lang.String,java.lang.String> getMapping()
    The mapping is taken out of the xmlBlaster.properties file. It is an Hashtable where the keys are the logical names used for a type in the JdbcQueuePlugin and the values are the real names to be used in the database used (which may be vendor specific).


    disconnect

    private final void disconnect(long waitTime,
                                  boolean silent)

    disconnect

    public void disconnect()
    Closes all connections to the Database. It should not throw any exception. Exceptions coming from the backend or the communication layer are catched and logged. When this occurs, the affected connections are set to null.


    finalize

    public void finalize()
    Overrides:
    finalize in class java.lang.Object

    getStatus

    public final int getStatus()
    returns true if the connection is temporarly lost (and the pool is polling for new connections)


    setDatabaseLost

    public final void setDatabaseLost()
    informs this pool that the connection to the DB has been lost


    getConnection

    public java.sql.Connection getConnection()
                                      throws XmlBlasterException
    Returns a free connection. If no connection is currently available it waits no more than what specified in the configuration. If after that time still no connection is available it throws an XmlBlasterException. It also throws an XmlBlasterException if the number of threads which are waiting is already too high (also configurable).

    Throws:
    XmlBlasterException

    releaseConnection

    public void releaseConnection(java.sql.Connection conn,
                                  boolean success)
                           throws XmlBlasterException
    Used to give back a connection to the pool. If the pool is already full it will throw an exception. If the success flag is true, it well release the connection back to the pool, otherwise it will throw away the current connection and add to the pool a new one.

    Throws:
    XmlBlasterException

    releaseConnection

    private void releaseConnection(java.sql.Connection conn)
                            throws XmlBlasterException
    Used to give back a connection to the pool. If the pool is already full it will throw an exception.

    Throws:
    XmlBlasterException

    dumpMetaData

    public void dumpMetaData()

    isDbAdmin

    public boolean isDbAdmin()

    getQueryTimeout

    public int getQueryTimeout()

    main

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

    shutdown

    public void shutdown()

    registerManager

    public void registerManager(java.lang.Object manager)

    unregisterManager

    public void unregisterManager(java.lang.Object manager)

    isBatchModeEnabled

    public boolean isBatchModeEnabled()
    The batch mode means that insertions in the database are made in batch mode, i.e. several entries in one sweep. This can increase perfomance significantly on some DBs.

    Returns:
    true if batch mode has been enabled, false otherwise (defaults to true).

    isCascadeDeleteSuppported

    public boolean isCascadeDeleteSuppported()
    Returns:
    true if 'cascadeDeleteSupported' has been set to true

    isNestedBracketsSuppported

    public boolean isNestedBracketsSuppported()
    Returns:
    true if 'nestedBracketsSupported' has been set to true

    getUrl

    public java.lang.String getUrl()
    Returns:
    the url

    xmlBlaster 2.2.0 API

    Copyright © 1999-2014 The xmlBlaster.org contributers.