xmlBlaster 2.2.0 client API

org.xmlBlaster.util.queue.jdbc
Class XBDatabaseAccessor

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

public class XBDatabaseAccessor
extends XBFactoryBase
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 class org.xmlBlaster.util.queue.jdbc.XBFactoryBase
DB2, FIREBIRD, HSQLDB, LDBC, MYSQL, ORACLE, POSTGRES, SQLITE, SQLSERVER_2000, SQLSERVER_2005, UNKNOWN
 
Fields inherited from interface org.xmlBlaster.util.queue.I_StorageProblemListener
AVAILABLE, UNAVAILABLE, UNDEF
 
Constructor Summary
XBDatabaseAccessor()
           
 
Method Summary
 int[] addEntries(XBStore store, I_Entry[] entries)
          Adds several rows to the specified queue table in batch mode to improve performance
 boolean addEntry(XBStore store, 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(XBStore store)
          Cleans up the specified queue.
 long clearQueue(XBStore store)
           
static XBDatabaseAccessor createInstance(Global glob, 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').
 long deleteAllTransient(XBStore store)
          deletes all transient messages
 long deleteEntries(XBStore store, XBRef[] refs, XBMeat[] meats)
          Deletes the entries specified by the entries array.
 int deleteEntry(XBStore store, long refId, long meatId)
          Deletes the entry specified
 long deleteFirstRefs(XBStore store, long numOfEntries)
          Deletes number of entries specified by the argument list.
 void deleteStore(long storeId)
          Deletes the store.
protected  void doInit(org.xmlBlaster.contrib.I_Info info)
           
 void dumpMetaData()
          Dumps the metadata to the log object.
 java.util.List<XBStore> getAllOfType(java.lang.String node, java.lang.String type)
           
 ReturnDataHolder getAndDeleteLowest(XBStore store, int numOfEntries, long numOfBytes, int maxPriority, long minUniqueId, boolean leaveOne, boolean doDelete, I_Storage storage)
          Under the same transaction it gets and deletes all the entries which fit into the constrains specified in the argument list.
 java.util.List<I_Entry> getEntries(XBStore store, int numOfEntries, long numOfBytes, I_EntryFilter entryFilter, boolean isRef, I_Storage storage)
          gets the first numOfEntries of the queue.
 java.util.List<I_Entry> getEntries(XBStore store, XBRef[] refs, XBMeat[] meats)
          gets all the entries which have the dataid specified in the argument list.
 java.util.List<I_Entry> getEntriesByPriority(XBStore store, int numOfEntries, long numOfBytes, int minPrio, int maxPrio)
          gets the first numOfEntries of the queue which have the priority in the range specified by prioMin and prioMax (inclusive).
 java.util.List<I_Entry> getEntriesBySamePriority(XBStore store, int numOfEntries, long numOfBytes)
          gets the first numOfEntries of the queue which have the same priority.
 XBEntry[] getEntriesLike(java.lang.String queueNamePattern, java.lang.String flag, int numOfEntries, long numOfBytes, I_EntryFilter entryFilter)
          Raw access to the table.
 java.util.List<I_Entry> getEntriesWithLimit(XBStore store, I_Entry limitEntry, I_Storage storage)
          gets the first numOfEntries of the queue until the limitEntry is reached.
static java.lang.String getIsolationLevel(java.sql.Connection conn)
           
 EntryCount getNumOfAll(XBStore store)
          Gets the real number of entries.
 org.xmlBlaster.contrib.db.I_DbPool getPool()
          Returns the pool associated to this object.
 java.util.List<I_Entry> getRefEntries(XBStore store, int numOfEntries, long numOfBytes, I_EntryFilter entryFilter, I_Storage storage, I_QueueEntry firstEntryExlusive)
           
 I_StorageProblemListener[] getStorageProblemListenerArr()
           
 XBStore getXBStore(StorageId uniqueQueueId)
           
 XBStoreFactory getXBStoreFactory()
           
 org.xmlBlaster.contrib.I_Info init(org.xmlBlaster.contrib.I_Info origInfo)
           
protected  boolean initFactory(Global global, PluginInfo plugInfo)
           
static java.lang.String isolationToString(int isolation)
           
 long modifyEntry(XBStore store, XBMeat entry, XBMeat oldEntry, boolean onlyRefCounters)
          modifies a row in the specified queue table
 boolean ping()
          pings the 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(XBStore store, XBRef limitEntry, boolean inclusive)
          deletes the first numOfEntries of the queue until the limitEntry is reached.
 void setUp(boolean deleteAllTransients)
          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).
 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 org.xmlBlaster.util.queue.jdbc.XBFactoryBase
getDbVendor
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XBDatabaseAccessor

public XBDatabaseAccessor()
Method Detail

initFactory

protected boolean initFactory(Global global,
                              PluginInfo plugInfo)
                       throws XmlBlasterException
Throws:
XmlBlasterException

init

public org.xmlBlaster.contrib.I_Info init(org.xmlBlaster.contrib.I_Info origInfo)
                                   throws XmlBlasterException
Overrides:
init in class XBFactoryBase
Throws:
XmlBlasterException

doInit

protected void doInit(org.xmlBlaster.contrib.I_Info info)
               throws XmlBlasterException
Specified by:
doInit in class XBFactoryBase
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

ping

public boolean ping()
pings the jdbc connection to check if the DB is up and running. It returns 'true' if the connection is OK, false otherwise. The ping is done by invocation


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

modifyEntry

public long modifyEntry(XBStore store,
                        XBMeat entry,
                        XBMeat oldEntry,
                        boolean onlyRefCounters)
                 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 boolean addEntry(XBStore store,
                        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(XBStore store,
                        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 int cleanUp(XBStore store)
            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

isolationToString

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

getIsolationLevel

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

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.


setUp

public final void setUp(boolean deleteAllTransients)
                 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 org.xmlBlaster.contrib.db.I_DbPool getPool()
Returns the pool associated to this object.

Returns:
JdbcConnectionPool the pool managing the connections to the DB

deleteAllTransient

public long deleteAllTransient(XBStore store)
                        throws XmlBlasterException
deletes all transient messages

Throws:
XmlBlasterException

getAndDeleteLowest

public ReturnDataHolder getAndDeleteLowest(XBStore store,
                                           int numOfEntries,
                                           long numOfBytes,
                                           int maxPriority,
                                           long minUniqueId,
                                           boolean leaveOne,
                                           boolean doDelete,
                                           I_Storage storage)
                                    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)

deleteStore

public void deleteStore(long storeId)
                 throws XmlBlasterException
Deletes the store.

Throws:
XmlBlasterException

deleteEntries

public long deleteEntries(XBStore store,
                          XBRef[] refs,
                          XBMeat[] meats)
                   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
refs - the array containing all entries to delete for the refs.
meats - the array containing all entries to delete for the meats.
Throws:
XmlBlasterException

deleteEntry

public int deleteEntry(XBStore store,
                       long refId,
                       long meatId)
                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

deleteFirstRefs

public long deleteFirstRefs(XBStore store,
                            long numOfEntries)
                     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.List<I_Entry> getEntriesByPriority(XBStore store,
                                                    int numOfEntries,
                                                    long numOfBytes,
                                                    int minPrio,
                                                    int maxPrio)
                                             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.List<I_Entry> getEntriesBySamePriority(XBStore store,
                                                        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

getRefEntries

public java.util.List<I_Entry> getRefEntries(XBStore store,
                                             int numOfEntries,
                                             long numOfBytes,
                                             I_EntryFilter entryFilter,
                                             I_Storage storage,
                                             I_QueueEntry firstEntryExlusive)
                                      throws XmlBlasterException
Throws:
XmlBlasterException

getEntries

public java.util.List<I_Entry> getEntries(XBStore store,
                                          int numOfEntries,
                                          long numOfBytes,
                                          I_EntryFilter entryFilter,
                                          boolean isRef,
                                          I_Storage storage)
                                   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 XBEntry[] 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.List<I_Entry> getEntriesWithLimit(XBStore store,
                                                   I_Entry limitEntry,
                                                   I_Storage storage)
                                            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(XBStore store,
                                   XBRef 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.List<I_Entry> getEntries(XBStore store,
                                          XBRef[] refs,
                                          XBMeat[] meats)
                                   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 EntryCount getNumOfAll(XBStore store)
                       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

createInstance

public static XBDatabaseAccessor createInstance(Global glob,
                                                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

shutdown

public void shutdown()

registerQueue

public void registerQueue(I_Queue queue)

unregisterQueue

public void unregisterQueue(I_Queue queue)

getXBStore

public XBStore getXBStore(StorageId uniqueQueueId)
                   throws XmlBlasterException
Throws:
XmlBlasterException

getAllOfType

public java.util.List<XBStore> getAllOfType(java.lang.String node,
                                            java.lang.String type)
                                     throws XmlBlasterException
Throws:
XmlBlasterException

clearQueue

public long clearQueue(XBStore store)
                throws XmlBlasterException
Throws:
XmlBlasterException

getXBStoreFactory

public XBStoreFactory getXBStoreFactory()

xmlBlaster 2.2.0 client API

Copyright © 1999-2014 The xmlBlaster.org contributers.