xmlBlaster 2.2.0 client API

org.xmlBlaster.util.queue.jdbc
Class CommonTableDatabaseAccessor

java.lang.Object
  extended by org.xmlBlaster.util.queue.jdbc.CommonTableDatabaseAccessor
All Implemented Interfaces:
I_StorageProblemListener, I_StorageProblemNotifier
Direct Known Subclasses:
CommonTableDatabaseAccessorDelegate

public class CommonTableDatabaseAccessor
extends java.lang.Object
implements I_StorageProblemListener, I_StorageProblemNotifier

Delegate class which takes care of SQL specific stuff for the JdbcQueuePlugin class.

One instance of this is created by Global for each StorageId prefix, so one instance for 'history', one for 'cb' etc.

The tables needed for each JdbcManagerCommonTable instance are the following:
- entriesTableName (defaults to 'ENTRIES')
The names of such tables are constituted by the tableNamePrefix (which defaults to 'XB') plus the entriesTableName

Author:
Marcel Ruff, Michele Laghi
See Also:
The queue.jdbc.commontable requirement

Field Summary
 
Fields inherited from interface org.xmlBlaster.util.queue.I_StorageProblemListener
AVAILABLE, UNAVAILABLE, UNDEF
 
Constructor Summary
CommonTableDatabaseAccessor(JdbcConnectionPool pool, I_EntryFactory factory, java.lang.String managerName, I_Storage storage)
           
 
Method Summary
 int[] addEntries(java.lang.String queueName, I_Entry[] entries)
          Adds several rows to the specified queue table in batch mode to improve performance
 boolean addEntry(java.lang.String queueName, I_Entry entry)
          Adds a row to the specified queue table
protected  boolean checkIfDBLoss(java.sql.Connection conn, java.lang.String location, java.lang.Throwable ex)
           
protected  boolean checkIfDBLoss(java.sql.Connection conn, java.lang.String location, java.lang.Throwable ex, java.lang.String trace)
          Handles the SQLException.
 int cleanUp(java.lang.String queueName)
          Cleans up the specified queue.
static CommonTableDatabaseAccessor createInstance(Global glob, I_EntryFactory factory, java.lang.String confType, java.lang.String confVersion, java.util.Properties properties)
          The Properties to use as a default are these from the QueuePlugin with the configuration name specified by defaultConfName (default is 'JDBC').
 int deleteAllTransient(java.lang.String queueName)
          deletes all transient messages
 boolean[] deleteEntries(java.lang.String queueName, long[] uniqueIds)
          Deletes the entries specified by the entries array.
 int deleteEntry(java.lang.String queueName, long uniqueId)
          Deletes the entry specified
 ReturnDataHolder deleteFirstEntries(java.lang.String queueName, long numOfEntries, long amount)
          Deletes number of entries specified by the argument list.
 void dumpMetaData()
          Dumps the metadata to the log object.
 ReturnDataHolder getAndDeleteLowest(StorageId storageId, int numOfEntries, long numOfBytes, int maxPriority, long minUniqueId, boolean leaveOne, boolean doDelete)
          Under the same transaction it gets and deletes all the entries which fit into the constrains specified in the argument list.
 java.util.ArrayList getEntries(StorageId storageId, int numOfEntries, long numOfBytes, I_EntryFilter entryFilter)
          gets the first numOfEntries of the queue.
 java.util.ArrayList getEntries(StorageId storageId, long[] dataids)
          gets all the entries which have the dataid specified in the argument list.
 java.util.ArrayList getEntriesByPriority(StorageId storageId, int numOfEntries, long numOfBytes, int minPrio, int maxPrio, I_EntryFilter entryFilter)
          gets the first numOfEntries of the queue which have the priority in the range specified by prioMin and prioMax (inclusive).
 java.util.ArrayList getEntriesBySamePriority(StorageId storageId, int numOfEntries, long numOfBytes)
          gets the first numOfEntries of the queue which have the same priority.
 java.util.ArrayList getEntriesLike(java.lang.String queueNamePattern, java.lang.String flag, int numOfEntries, long numOfBytes, I_EntryFilter entryFilter)
          Raw access to the table.
 java.util.ArrayList getEntriesWithLimit(StorageId storageId, I_Entry limitEntry)
          gets the first numOfEntries of the queue until the limitEntry is reached.
 EntryCount getNumOfAll(java.lang.String queueName)
          Gets the real number of entries.
 long getNumOfBytes(java.lang.String queueName)
          Deprecated. Use getNumOfAll(String)
 long getNumOfEntries(java.lang.String queueName)
          Deprecated. Use getNumOfAll(String)
 long getNumOfPersistents(java.lang.String queueName)
          Deprecated. Use getNumOfAll(String)
 JdbcConnectionPool getPool()
          Returns the pool associated to this object.
 long getSizeOfPersistents(java.lang.String queueName)
          Deprecated. Use getNumOfAll(String) Replaced all four selects with one: "select count(*), sum(byteSize), durable from XB_ENTRIES WHERE queueName='history_xmlBlaster_192_168_1_4_3412Hello' GROUP BY durable;"
 I_StorageProblemListener[] getStorageProblemListenerArr()
           
 boolean isDB2()
           
 boolean isFirebird()
           
 boolean isHSQLDatabaseEngine()
           
 boolean isLdbc()
           
 boolean isMicrosoftSQLServer()
           
 boolean isMySql()
           
 boolean isOracle()
           
 boolean isPostgres()
           
 boolean isSQLite()
           
static void main(java.lang.String[] args)
          This main method can be used to delete all tables on the db which start with a certain prefix.
 long modifyEntry(java.lang.String queueName, I_Entry entry, I_Entry oldEntry)
          modifies a row in the specified queue table
 boolean pingDatabase()
          pings with a newly created jdbc connection to check if the DB is up and running.
 void registerQueue(I_Queue queue)
           
 boolean registerStorageProblemListener(I_StorageProblemListener entry)
          Adds (registers) a listener for connection/disconnection events.
 long removeEntriesWithLimit(StorageId storageId, I_Entry limitEntry, boolean inclusive)
          deletes the first numOfEntries of the queue until the limitEntry is reached.
 void setUp()
          Sets up a table for the queue specified by this queue name.
 void shutdown()
           
 void storageAvailable(int oldStatus)
          Invoked by the I_StorageProblemNotifier when the storage becomes available again (for example on a DB when the jdbc connection is broken).
 void storageUnavailable(int oldStatus)
          Invoked by the I_StorageProblemNotifier when the storage becomes unavailable (for example on a DB when the jdbc connection is broken).
 boolean tablesCheckAndSetup(boolean createTables)
          Checks if all necessary tables exist.
 void unregisterQueue(I_Queue queue)
           
 boolean unRegisterStorageProblemListener(I_StorageProblemListener entry)
          Adds (registers) a listener for connection/disconnection events.
 int wipeOutDB(boolean doSetupNewTables)
          Wipes out the entire DB.
static void wipeOutDB(Global glob, java.lang.String confType, java.lang.String confVersion, java.util.Properties properties, boolean setupNewTables)
          wipes out the db.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommonTableDatabaseAccessor

public CommonTableDatabaseAccessor(JdbcConnectionPool pool,
                                   I_EntryFactory factory,
                                   java.lang.String managerName,
                                   I_Storage storage)
                            throws XmlBlasterException
Parameters:
storage - TODO
JdbcConnectionPool - the pool to be used for the connections to the database. IMPORTANT: The pool must have been previously initialized.
Throws:
XmlBlasterException
Method Detail

pingDatabase

public boolean pingDatabase()
pings with a newly created jdbc connection to check if the DB is up and running. It returns 'true' if the database is available, false otherwise. The ping is done by invocation

We don't use a connection from the pool for this check as it could be stale even if the database is alive.


registerStorageProblemListener

public boolean registerStorageProblemListener(I_StorageProblemListener entry)
Adds (registers) a listener for connection/disconnection events.

Specified by:
registerStorageProblemListener in interface I_StorageProblemNotifier

unRegisterStorageProblemListener

public boolean unRegisterStorageProblemListener(I_StorageProblemListener entry)
Adds (registers) a listener for connection/disconnection events.

Specified by:
unRegisterStorageProblemListener in interface I_StorageProblemNotifier
Returns:
boolean true if the entry was successfully removed.

storageUnavailable

public void storageUnavailable(int oldStatus)
Description copied from interface: I_StorageProblemListener
Invoked by the I_StorageProblemNotifier when the storage becomes unavailable (for example on a DB when the jdbc connection is broken).

Specified by:
storageUnavailable in interface I_StorageProblemListener
Parameters:
oldStatus - the status before the storage became unavailable.
See Also:
I_StorageProblemListener.storageUnavailable(int)

storageAvailable

public void storageAvailable(int oldStatus)
Description copied from interface: I_StorageProblemListener
Invoked by the I_StorageProblemNotifier when the storage becomes available again (for example on a DB when the jdbc connection is broken). Note that this method is invoked ONLY after the connection has become unavailable, it is NOT invoked the at startup, i.e. the first time the connection becomes available.

Specified by:
storageAvailable in interface I_StorageProblemListener
See Also:
I_StorageProblemListener.storageAvailable(int)

getStorageProblemListenerArr

public I_StorageProblemListener[] getStorageProblemListenerArr()
Returns:
A current snapshot of the connection listeners where we can work on (unsynchronized) and remove listeners without danger

checkIfDBLoss

protected final boolean checkIfDBLoss(java.sql.Connection conn,
                                      java.lang.String location,
                                      java.lang.Throwable ex)
See Also:
checkIfDBLoss(Connection, String, Throwable, String)

checkIfDBLoss

protected final boolean checkIfDBLoss(java.sql.Connection conn,
                                      java.lang.String location,
                                      java.lang.Throwable ex,
                                      java.lang.String trace)
Handles the SQLException. If it is a communication exception like the connection has been broken it will inform the connection pool.

Parameters:
location - where the exception occured.
ex - the exception which has to be handled.
trace - additional information to put in the logetLogId(tableName, storageId, "getEntries") logging trace.
Returns:
boolean true if it was a communication exception

tablesCheckAndSetup

public final boolean tablesCheckAndSetup(boolean createTables)
                                  throws XmlBlasterException
Checks if all necessary tables exist. If a table does not exist and 'createTables' true, then the table is created.

Returns:
boolean 'true' if the tables are all there after the invocation to this method, 'false' if at least one of the required tables is missing after the invocation to this method.
Throws:
XmlBlasterException

modifyEntry

public long modifyEntry(java.lang.String queueName,
                        I_Entry entry,
                        I_Entry oldEntry)
                 throws XmlBlasterException
modifies a row in the specified queue table

Parameters:
queueName - The name of the queue on which to perform the operation
entry - the object to be stored.
oldEntry - the old one
Returns:
true on success
Throws:
XmlBlasterException - if an error occurred when trying to get a connection or an SQLException occurred.

addEntry

public final boolean addEntry(java.lang.String queueName,
                              I_Entry entry)
                       throws XmlBlasterException
Adds a row to the specified queue table

Parameters:
queueName - The name of the queue on which to perform the operation
entry - the object to be stored.
Returns:
true on success
Throws:
java.sql.SQLException - if an error occured while adding the row
XmlBlasterException - if an error occured when trying to get a connection

addEntries

public int[] addEntries(java.lang.String queueName,
                        I_Entry[] entries)
                 throws XmlBlasterException
Adds several rows to the specified queue table in batch mode to improve performance

Parameters:
queueName - The name of the queue on which to perform the operation
entries - the entries to store
Returns:
array of boolean telling which entries where stored and which not.
Throws:
java.sql.SQLException - if an error occured while adding the row
XmlBlasterException - if an error occured when trying to get a connection

cleanUp

public final int cleanUp(java.lang.String queueName)
                  throws XmlBlasterException
Cleans up the specified queue. It deletes all queue entries in the 'entries' table.

Returns:
the number of queues deleted (not the number of entries).
Throws:
XmlBlasterException

wipeOutDB

public int wipeOutDB(boolean doSetupNewTables)
              throws XmlBlasterException
Wipes out the entire DB. i.e. it deletes the three tables from the DB. IMPORTANT:

Parameters:
doSetupNewTables - if set to 'true' it recreates the necessary empty tables.
Throws:
XmlBlasterException

dumpMetaData

public void dumpMetaData()
Dumps the metadata to the log object. The log will write out this information as an info. This means you don't need to have the switch 'dump' set to true to see this information.


getNumOfBytes

public long getNumOfBytes(java.lang.String queueName)
                   throws XmlBlasterException
Deprecated. Use getNumOfAll(String)

returns the amount of bytes currently in the specified queue

Parameters:
tableName - the name of the table in which to count
Returns:
the current amount of bytes used in the table.
Throws:
XmlBlasterException

setUp

public final void setUp()
                 throws XmlBlasterException
Sets up a table for the queue specified by this queue name. If one already exists (i.e. if it recovers from a crash) its associated table name is returned.

Throws:
XmlBlasterException

getPool

public JdbcConnectionPool getPool()
Returns the pool associated to this object.

Returns:
JdbcConnectionPool the pool managing the connections to the DB

deleteAllTransient

public int deleteAllTransient(java.lang.String queueName)
                       throws XmlBlasterException
deletes all transient messages

Throws:
XmlBlasterException

getAndDeleteLowest

public ReturnDataHolder getAndDeleteLowest(StorageId storageId,
                                           int numOfEntries,
                                           long numOfBytes,
                                           int maxPriority,
                                           long minUniqueId,
                                           boolean leaveOne,
                                           boolean doDelete)
                                    throws XmlBlasterException
Under the same transaction it gets and deletes all the entries which fit into the constrains specified in the argument list. The entries are really deleted only if doDelete is true, otherwise they are left untouched on the queue

Throws:
XmlBlasterException
See Also:
I_Queue.takeLowest(int, long, org.xmlBlaster.util.queue.I_QueueEntry, boolean)

deleteEntries

public boolean[] deleteEntries(java.lang.String queueName,
                               long[] uniqueIds)
                        throws XmlBlasterException
Deletes the entries specified by the entries array. Since all entries are deleted at the same time, in case of an exception the result is uncertain (it depends on what the used database will do). In case the statement would be too long for the DB, the request is split into several statements which are invoked separately.

Parameters:
tableName - the name of the table on which to delete the entries
uniqueIds - the array containing all the uniqueId for the entries to delete.
Throws:
XmlBlasterException

deleteEntry

public int deleteEntry(java.lang.String queueName,
                       long uniqueId)
                throws XmlBlasterException
Deletes the entry specified

Parameters:
tableName - the name of the table on which to delete the entries
uniqueIds - the array containing all the uniqueId for the entries to delete.
Throws:
XmlBlasterException

deleteFirstEntries

public ReturnDataHolder deleteFirstEntries(java.lang.String queueName,
                                           long numOfEntries,
                                           long amount)
                                    throws XmlBlasterException
Deletes number of entries specified by the argument list. If less than that amount of elements is given, then all entries will be deleted. If you specify -1 as the number of entries, then all entries will be deleted.

Parameters:
tableName - the name of the table on which to act.
numOfEntries - the number of entries to be deleted.
amount - the maximum amount of bytes to remove. Note that if no entries fit into this size, the first entry is taken anyway (to avoid deadlocks)
Throws:
XmlBlasterException

getEntriesByPriority

public java.util.ArrayList getEntriesByPriority(StorageId storageId,
                                                int numOfEntries,
                                                long numOfBytes,
                                                int minPrio,
                                                int maxPrio,
                                                I_EntryFilter entryFilter)
                                         throws XmlBlasterException
gets the first numOfEntries of the queue which have the priority in the range specified by prioMin and prioMax (inclusive). If there are not so many entries in the queue, all elements in the queue are returned.

Parameters:
storageId - the storageId of the queue/storage from which to retrieve the information.
numOfEntries - the maximum number of elements to retrieve. If negative there is no constriction.
numOfBytes - the maximum number of bytes to retrieve. If negative, there is no constriction.
minPrio - the minimum priority to retreive (inclusive).
maxPrio - the maximum priority to retrieve (inclusive).
Throws:
XmlBlasterException

getEntriesBySamePriority

public java.util.ArrayList getEntriesBySamePriority(StorageId storageId,
                                                    int numOfEntries,
                                                    long numOfBytes)
                                             throws XmlBlasterException
gets the first numOfEntries of the queue which have the same priority. If there are not so many entries in the queue, all elements in the queue are returned.

Parameters:
numOfEntries - the maximum number of elements to retrieve
Throws:
XmlBlasterException

getEntries

public java.util.ArrayList getEntries(StorageId storageId,
                                      int numOfEntries,
                                      long numOfBytes,
                                      I_EntryFilter entryFilter)
                               throws XmlBlasterException
gets the first numOfEntries of the queue. If there are not so many entries in the queue, all elements in the queue are returned.

Is public for testsuite only.

Parameters:
numOfEntries - Access num entries, if -1 access all entries currently found
numOfBytes - is the maximum size in bytes of the array to return, -1 is unlimited .
Throws:
XmlBlasterException

getEntriesLike

public java.util.ArrayList getEntriesLike(java.lang.String queueNamePattern,
                                          java.lang.String flag,
                                          int numOfEntries,
                                          long numOfBytes,
                                          I_EntryFilter entryFilter)
                                   throws XmlBlasterException
Raw access to the table. If there are not so many entries in the queue, all elements in the queue are returned.

Parameters:
numOfEntries - the maximum number of elements to retrieve
Throws:
XmlBlasterException

getEntriesWithLimit

public java.util.ArrayList getEntriesWithLimit(StorageId storageId,
                                               I_Entry limitEntry)
                                        throws XmlBlasterException
gets the first numOfEntries of the queue until the limitEntry is reached.

Parameters:
numOfEntries - the maximum number of elements to retrieve
Throws:
XmlBlasterException

removeEntriesWithLimit

public long removeEntriesWithLimit(StorageId storageId,
                                   I_Entry limitEntry,
                                   boolean inclusive)
                            throws XmlBlasterException
deletes the first numOfEntries of the queue until the limitEntry is reached.

Parameters:
numOfEntries - the maximum number of elements to retrieve
Throws:
XmlBlasterException

getEntries

public java.util.ArrayList getEntries(StorageId storageId,
                                      long[] dataids)
                               throws XmlBlasterException
gets all the entries which have the dataid specified in the argument list. If the list is empty or null, an empty ArrayList object is returned.

Throws:
XmlBlasterException

getNumOfAll

public final EntryCount getNumOfAll(java.lang.String queueName)
                             throws XmlBlasterException
Gets the real number of entries. That is it really makes a call to the DB to find out how big the size is.

Returns:
never null
Throws:
XmlBlasterException

getNumOfEntries

public final long getNumOfEntries(java.lang.String queueName)
                           throws XmlBlasterException
Deprecated. Use getNumOfAll(String)

Gets the real number of entries. That is it really makes a call to the DB to find out how big the size is.

Throws:
XmlBlasterException

getNumOfPersistents

public final long getNumOfPersistents(java.lang.String queueName)
                               throws XmlBlasterException
Deprecated. Use getNumOfAll(String)

Gets the real number of persistent entries. That is it really makes a call to the DB to find out how big the size is.

Throws:
XmlBlasterException

getSizeOfPersistents

public final long getSizeOfPersistents(java.lang.String queueName)
                                throws XmlBlasterException
Deprecated. Use getNumOfAll(String) Replaced all four selects with one: "select count(*), sum(byteSize), durable from XB_ENTRIES WHERE queueName='history_xmlBlaster_192_168_1_4_3412Hello' GROUP BY durable;"

gets the real size of persistent entries, that is it really makes a call to the DB to find out how big the size is.

Throws:
XmlBlasterException

createInstance

public static CommonTableDatabaseAccessor createInstance(Global glob,
                                                         I_EntryFactory factory,
                                                         java.lang.String confType,
                                                         java.lang.String confVersion,
                                                         java.util.Properties properties)
                                                  throws XmlBlasterException
The Properties to use as a default are these from the QueuePlugin with the configuration name specified by defaultConfName (default is 'JDBC'). You can overwrite these properties entirely or partially with 'properties'.

Parameters:
confType - the name of the configuration to use as default. If you pass null, then 'JDBC' will be taken.
confVersion - the version to use as a default. If you pass null, then '1.0' will be taken.
properties - the properties to use to overwrite the default properties. If you pass null, no properties will be overwritten, and the default will be used.
Throws:
XmlBlasterException

wipeOutDB

public static void wipeOutDB(Global glob,
                             java.lang.String confType,
                             java.lang.String confVersion,
                             java.util.Properties properties,
                             boolean setupNewTables)
                      throws XmlBlasterException
wipes out the db. The Properties to use as a default are these from the QueuePlugin with the configuration name specified by defaultConfName (default is 'JDBC'). You can overwrite these properties entirely or partially with 'properties'.

Parameters:
confType - the name of the configuration to use as default. If you pass null, then 'JDBC' will be taken.
confVersion - the version to use as a default. If you pass null, then '1.0' will be taken.
properties - the properties to use to overwrite the default properties. If you pass null, no properties will be overwritten, and the default will be used.
setupNewTables - tells the manager to recreate empty tables if set to 'true'. Note that this flag only has effect if the JdbcManagerCommonTable is used.
Throws:
XmlBlasterException

main

public static void main(java.lang.String[] args)
This main method can be used to delete all tables on the db which start with a certain prefix. It is useful to cleanup the entire DB.


shutdown

public void shutdown()

registerQueue

public void registerQueue(I_Queue queue)

unregisterQueue

public void unregisterQueue(I_Queue queue)

isDB2

public boolean isDB2()
Returns:
the isDb2

isHSQLDatabaseEngine

public boolean isHSQLDatabaseEngine()
Returns:
the isHsqlDb

isMicrosoftSQLServer

public boolean isMicrosoftSQLServer()
Returns:
the isMsSqlServer

isMySql

public boolean isMySql()
Returns:
the isMySql

isOracle

public boolean isOracle()
Returns:
the isOracle

isPostgres

public boolean isPostgres()
Returns:
the isPostgres

isFirebird

public boolean isFirebird()
Returns:
the firebird

isLdbc

public boolean isLdbc()
Returns:
the isLdbc

isSQLite

public boolean isSQLite()
Returns:
the isSQLite

xmlBlaster 2.2.0 client API

Copyright © 1999-2014 The xmlBlaster.org contributers.