org.sandev.tools.JDBC
Class THD_JDBCPersister

java.lang.Object
  extended byorg.sandev.tools.JDBC.THD_JDBCPersister
All Implemented Interfaces:
org.sandev.basics.util.IDLookup, org.sandev.basics.DataManager.Persister

public class THD_JDBCPersister
extends java.lang.Object
implements org.sandev.basics.DataManager.Persister

A Persister implementation that communicates with a database via JDBC. AUTOGENERATED FILE, DO NOT EDIT DIRECTLY.
source: declared in build
generator: org.sandev.generator.JDBCPersisterGenerator
time: Dec 2, 2005 6:38:59 PM
This implementation relies on the container to provide a DataSource via JNDI, and to provide connection pooling. If no UniqueIDManager is specified in the DataManager, then a TableKeyCounter is used.


Field Summary
protected  org.sandev.basics.DataManager.UniqueIDManager counter
          We keep track of what the next primary key for each table should be using an in memory counter.
protected  org.sandev.basics.DataManager.DataManagerNode dm
          Our back reference to the DataManagerNode.
protected  java.sql.Driver driver
          Local Driver reference used by getDirectConnection
protected  org.sandev.basics.util.InstanceClassEnumerator instClassEnum
          Our local InstanceClassEnumerator used for instance lookup.
protected  java.util.Properties properties
          Local Properties reference used by getDirectConnection
 
Fields inherited from interface org.sandev.basics.DataManager.Persister
TEMPIDTHRESHOLD
 
Constructor Summary
THD_JDBCPersister()
           
 
Method Summary
protected  java.lang.String cleanDBExceptionText(java.sql.SQLException e, org.sandev.basics.structs.SandUpdateMessage sum)
          Utility method for converting the SQLException we got back into something that makes more sense to an end user.
static void closeConnection(java.sql.Connection conn)
          Convenience method for closing a database connection.
static void closeConnection(java.sql.Statement stmt, java.sql.Connection conn)
          Convenience method for closing a statement and a connection.
protected  void debugDumpContext(javax.naming.InitialContext ctx)
          Dump the contents of the given InitialContext if possible.
protected  org.sandev.basics.structs.AggregateUpdate doAggregateUpdate(org.sandev.basics.structs.AggregateUpdate msg)
          Process all the updates within a single transaction.
protected  org.sandev.basics.structs.SandUpdateMessage doCoreUpdate(org.sandev.basics.structs.SandUpdateMessage msg, java.sql.Connection conn)
          thin wrapper for THD_UpdateProcessor.doUpdate
protected  org.sandev.basics.sandmessages.DataImportExport doExport(org.sandev.basics.sandmessages.DataImportExport msg)
          Export workhorse for doImportExport.
protected  org.sandev.basics.sandmessages.DataImportExport doImport(org.sandev.basics.sandmessages.DataImportExport msg)
          Import workhorse for doImportExport.
protected  org.sandev.basics.sandmessages.DataImportExport doImportExport(org.sandev.basics.sandmessages.DataImportExport msg)
          Deal with the import or export request and return the results.
protected  org.sandev.basics.structs.SandCollectionMessage doQuery(org.sandev.basics.structs.SandQueryMessage msg)
          thin wrapper for THD_QueryProcessor.doQuery
protected  org.sandev.basics.structs.SandUpdateMessage doUpdate(org.sandev.basics.structs.SandUpdateMessage msg)
          Process the update.
 java.sql.Connection getConnection()
          Non verbose callthrough to getConnection.
 java.sql.Connection getConnection(boolean verbose)
          Returns a database connection for this Persister.
 org.sandev.basics.DataManager.UniqueIDManager getCounter()
          accessor for counter
 org.sandev.basics.DataManager.DataManagerNode getDataManager()
          accessor for dm
static java.lang.String getDescription(org.sandev.basics.structs.SandPersistMessage spm)
          Return a description of this message suitable for logging.
 java.sql.Connection getDirectConnection(java.lang.String dsname, boolean verbose)
          Retrieve a Connection through the specified driver, url, and access info.
 org.sandev.basics.structs.SandPersistMessage getInstance(java.lang.String classname, long id, org.sandev.basics.util.AuthUser user)
          Convert the classname id combination into an equivalent query message and call
protected  org.sandev.basics.util.InstanceClassEnumerator getInstClassEnum()
          Initializing accessor for instClassEnum
 java.sql.Connection getJNDILookupConnection(java.lang.String dsname, boolean verbose)
          Retrieve a Connection by finding the DataSource JNDI binding
protected  long getMessageID(org.sandev.basics.structs.SandUpdateMessage sum)
          Gets the instance out of the given update message, downcasts it to a SandPeristMessage and returns its uniqueID.
protected  void info(java.lang.String text)
          Shorthand method for logging informational text.
 void init(org.sandev.basics.DataManager.DataManagerNode datamgr)
          Setup for work.
protected  void initDriver(java.lang.String drivername)
          Initializes our driver reference given a driver class name
protected  void initProperties(java.lang.String username, java.lang.String password)
          Initialized our properties with the username and password given.
 org.sandev.basics.structs.SandMessage processMessage(org.sandev.basics.structs.SandMessage msg)
          Handle AggregateUpdate, SandUpdateMessage or SandQueryMessage.
 void setCounter(org.sandev.basics.DataManager.UniqueIDManager mgr)
          mutator for counter
 void setDataManager(org.sandev.basics.DataManager.DataManagerNode dm)
          mutator for dm
protected  void setMessageID(org.sandev.basics.structs.SandUpdateMessage sum, long val)
          Gets the instance out of the given update message, downcasts it to a SandPeristMessage and sets its uniqueID.
static void verifyInitialData(org.sandev.basics.DataManager.DataManagerNode dm, java.sql.Connection conn)
          Get our initial data from the configuration and make sure that everything exists as specified.
static void wrappedLog(org.sandev.basics.nodecommon.Logger logger, int loglevel, java.lang.String msg)
          Wraps the logger call to throw a SQLException instead.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dm

protected org.sandev.basics.DataManager.DataManagerNode dm
Our back reference to the DataManagerNode. Set at initialization.


counter

protected org.sandev.basics.DataManager.UniqueIDManager counter
We keep track of what the next primary key for each table should be using an in memory counter. This is initialized by reading from the database at startup. The assumption here (big one) is that there will only be one Persister instance in this deployment.


instClassEnum

protected org.sandev.basics.util.InstanceClassEnumerator instClassEnum
Our local InstanceClassEnumerator used for instance lookup.


driver

protected java.sql.Driver driver
Local Driver reference used by getDirectConnection


properties

protected java.util.Properties properties
Local Properties reference used by getDirectConnection

Constructor Detail

THD_JDBCPersister

public THD_JDBCPersister()
Method Detail

getDataManager

public org.sandev.basics.DataManager.DataManagerNode getDataManager()
accessor for dm


setDataManager

public void setDataManager(org.sandev.basics.DataManager.DataManagerNode dm)
mutator for dm


getCounter

public org.sandev.basics.DataManager.UniqueIDManager getCounter()
accessor for counter


setCounter

public void setCounter(org.sandev.basics.DataManager.UniqueIDManager mgr)
mutator for counter


init

public void init(org.sandev.basics.DataManager.DataManagerNode datamgr)
          throws org.sandev.basics.DataManager.PersisterException
Setup for work. Verify our tables and initial data, then set our internal ID counter.

Specified by:
init in interface org.sandev.basics.DataManager.Persister
Throws:
org.sandev.basics.DataManager.PersisterException

processMessage

public org.sandev.basics.structs.SandMessage processMessage(org.sandev.basics.structs.SandMessage msg)
                                                     throws org.sandev.basics.DataManager.PersisterException
Handle AggregateUpdate, SandUpdateMessage or SandQueryMessage. Any other kind of message will trigger an exception. This just switches to call doAggregateUpdate, doUpdate or doQuery.

Specified by:
processMessage in interface org.sandev.basics.DataManager.Persister
Throws:
org.sandev.basics.DataManager.PersisterException

getInstClassEnum

protected org.sandev.basics.util.InstanceClassEnumerator getInstClassEnum()
Initializing accessor for instClassEnum


getInstance

public org.sandev.basics.structs.SandPersistMessage getInstance(java.lang.String classname,
                                                                long id,
                                                                org.sandev.basics.util.AuthUser user)
                                                         throws org.sandev.basics.structs.SandException
Convert the classname id combination into an equivalent query message and call

Specified by:
getInstance in interface org.sandev.basics.util.IDLookup
Throws:
org.sandev.basics.structs.SandException

wrappedLog

public static void wrappedLog(org.sandev.basics.nodecommon.Logger logger,
                              int loglevel,
                              java.lang.String msg)
                       throws java.sql.SQLException
Wraps the logger call to throw a SQLException instead.

Throws:
java.sql.SQLException

verifyInitialData

public static void verifyInitialData(org.sandev.basics.DataManager.DataManagerNode dm,
                                     java.sql.Connection conn)
                              throws java.sql.SQLException
Get our initial data from the configuration and make sure that everything exists as specified.

Throws:
java.sql.SQLException

getDescription

public static java.lang.String getDescription(org.sandev.basics.structs.SandPersistMessage spm)
Return a description of this message suitable for logging.


getMessageID

protected long getMessageID(org.sandev.basics.structs.SandUpdateMessage sum)
Gets the instance out of the given update message, downcasts it to a SandPeristMessage and returns its uniqueID.


setMessageID

protected void setMessageID(org.sandev.basics.structs.SandUpdateMessage sum,
                            long val)
Gets the instance out of the given update message, downcasts it to a SandPeristMessage and sets its uniqueID.


doAggregateUpdate

protected org.sandev.basics.structs.AggregateUpdate doAggregateUpdate(org.sandev.basics.structs.AggregateUpdate msg)
                                                               throws org.sandev.basics.DataManager.PersisterException
Process all the updates within a single transaction. Calls doCoreUpdate for the main work.

Throws:
org.sandev.basics.DataManager.PersisterException

doUpdate

protected org.sandev.basics.structs.SandUpdateMessage doUpdate(org.sandev.basics.structs.SandUpdateMessage msg)
                                                        throws org.sandev.basics.DataManager.PersisterException
Process the update. Calls through to doCoreUpdate.

Throws:
org.sandev.basics.DataManager.PersisterException

doCoreUpdate

protected org.sandev.basics.structs.SandUpdateMessage doCoreUpdate(org.sandev.basics.structs.SandUpdateMessage msg,
                                                                   java.sql.Connection conn)
                                                            throws java.sql.SQLException
thin wrapper for THD_UpdateProcessor.doUpdate

Throws:
java.sql.SQLException

doQuery

protected org.sandev.basics.structs.SandCollectionMessage doQuery(org.sandev.basics.structs.SandQueryMessage msg)
                                                           throws org.sandev.basics.DataManager.PersisterException
thin wrapper for THD_QueryProcessor.doQuery

Throws:
org.sandev.basics.DataManager.PersisterException

doImportExport

protected org.sandev.basics.sandmessages.DataImportExport doImportExport(org.sandev.basics.sandmessages.DataImportExport msg)
                                                                  throws org.sandev.basics.DataManager.PersisterException
Deal with the import or export request and return the results. Export just loads all the query results into a giant array and then writes it all to file, import reads the whole file into memory and then goes to work on it. Paging of data is not handled and will throw an exception if needed.

Throws:
org.sandev.basics.DataManager.PersisterException

doExport

protected org.sandev.basics.sandmessages.DataImportExport doExport(org.sandev.basics.sandmessages.DataImportExport msg)
                                                            throws org.sandev.basics.DataManager.PersisterException
Export workhorse for doImportExport.

Throws:
org.sandev.basics.DataManager.PersisterException

doImport

protected org.sandev.basics.sandmessages.DataImportExport doImport(org.sandev.basics.sandmessages.DataImportExport msg)
                                                            throws org.sandev.basics.DataManager.PersisterException
Import workhorse for doImportExport.

Throws:
org.sandev.basics.DataManager.PersisterException

getConnection

public java.sql.Connection getConnection()
                                  throws org.sandev.basics.DataManager.PersisterException
Non verbose callthrough to getConnection.

Throws:
org.sandev.basics.DataManager.PersisterException

getConnection

public java.sql.Connection getConnection(boolean verbose)
                                  throws org.sandev.basics.DataManager.PersisterException
Returns a database connection for this Persister. We call DataManagerNode.getDataSource to retrieve the data source to use. If the source starts with "direct:" then we parse the driver directly, otherwise we look up an associated DataSource object via JNDI.

Throws:
org.sandev.basics.DataManager.PersisterException

initDriver

protected void initDriver(java.lang.String drivername)
                   throws org.sandev.basics.DataManager.PersisterException
Initializes our driver reference given a driver class name

Throws:
org.sandev.basics.DataManager.PersisterException

initProperties

protected void initProperties(java.lang.String username,
                              java.lang.String password)
                       throws org.sandev.basics.DataManager.PersisterException
Initialized our properties with the username and password given.

Throws:
org.sandev.basics.DataManager.PersisterException

getDirectConnection

public java.sql.Connection getDirectConnection(java.lang.String dsname,
                                               boolean verbose)
                                        throws org.sandev.basics.DataManager.PersisterException
Retrieve a Connection through the specified driver, url, and access info.

Throws:
org.sandev.basics.DataManager.PersisterException

getJNDILookupConnection

public java.sql.Connection getJNDILookupConnection(java.lang.String dsname,
                                                   boolean verbose)
                                            throws org.sandev.basics.DataManager.PersisterException
Retrieve a Connection by finding the DataSource JNDI binding

Throws:
org.sandev.basics.DataManager.PersisterException

debugDumpContext

protected void debugDumpContext(javax.naming.InitialContext ctx)
Dump the contents of the given InitialContext if possible. Useful for debugging database connectivity problems at startup. Does not throw, since this is being called from within exception processing. Uses System.out.println to avoid any potential logging issues.


closeConnection

public static void closeConnection(java.sql.Statement stmt,
                                   java.sql.Connection conn)
                            throws org.sandev.basics.DataManager.PersisterException
Convenience method for closing a statement and a connection.

Throws:
org.sandev.basics.DataManager.PersisterException

closeConnection

public static void closeConnection(java.sql.Connection conn)
                            throws org.sandev.basics.DataManager.PersisterException
Convenience method for closing a database connection.

Throws:
org.sandev.basics.DataManager.PersisterException

cleanDBExceptionText

protected java.lang.String cleanDBExceptionText(java.sql.SQLException e,
                                                org.sandev.basics.structs.SandUpdateMessage sum)
Utility method for converting the SQLException we got back into something that makes more sense to an end user. This method cannot and does not throw, since it is called during exception handling.

The approach here is to use the leverage the struct declarations to produce better and more consistent error messages. The source text for output is in the DataManagerNode constants.

Sample source error messages for illustration purposes:


info

protected void info(java.lang.String text)
Shorthand method for logging informational text.