com.objectwave.persist.broker
Class RDBConnection

java.lang.Object
  |
  +--com.objectwave.persist.broker.RDBConnection
Direct Known Subclasses:
OracleConnection

public class RDBConnection
extends java.lang.Object

A connection to the database. Used by the connection pool and the RDBBroker. A not null value for the system property ow.persistConnectionVerbose will result in details of the SQL being sent over this connection to be logged. There will only ever be one thread operating on a single instance of an RDBConnection. A violation of that assumption will destroy the thread safety of this class.

Version:
$Id: RDBConnection.java,v 2.5 2002/02/07 02:44:05 dave_hoag Exp $
Author:
Dave Hoag

Field Summary
protected  int cachedCount
           
protected  SqlStatementCacheItem[] cachedStatements
           
protected  java.sql.Statement cachedUpdateStmt
           
protected  java.sql.Connection connection
           
protected  SqlConnectionFactory connectionSource
           
protected  java.lang.String connectUrl
           
protected static boolean doInsert
           
protected static java.lang.String insrtProp
           
protected  boolean inTransaction
           
protected  GrinderResultSet lastResultSet
           
protected static boolean manageTransaction
          If manageTransaction is false, no commit or rollback calls will ever be made.
protected static boolean metrics
           
protected static java.lang.String metricsProp
           
protected  boolean needsExplicitBegin
           
protected  java.lang.Object[] objArrays
           
protected  java.lang.String password
           
protected  RDBConnectionPool pool
           
protected  ObjectPoolBroker poolBroker
           
static int sqlStatementCount
          Description of the Field
protected  boolean supportsTransaction
           
protected  java.lang.Thread thread
           
protected  java.lang.String userName
           
protected static boolean verbose
           
protected static java.lang.String verbProp
           
 
Method Summary
protected  void addStatement(SQLModifier modifier, java.lang.Class persistenceClass, java.sql.PreparedStatement stmt)
          Add a prepared statement to this connection's path.
 void alterVendorConnection(java.sql.Connection con)
          Callback hook from connectionSource to allow vendor specific modification to the connection object.
 void beginTransaction()
          If the database supports transactions this method would begin the transaction.
protected  java.sql.Connection checkConnection()
          Verify that there is already a connection to the relational database.
protected  void clearConnection()
          Only in the event of a very bad thing should this method be invoked.
 void commit()
          Issues a database Commit.
 void execSql(java.lang.String aString)
          Execute and finish.
 java.util.List executeQuery(java.lang.String aString, int numberOfColumns)
          Execute and finish.
protected  void finalize()
          Disconnect from database if not already done.
 GrinderResultSet findExecSql(SQLAssembler sqlObj)
          Used for executing any sql query statement.
 GrinderResultSet findExecSql(SQLAssembler sqlObj, boolean release)
          Used for executing any sql query statement.
 void freeConnection()
          Every connection is associated with a particular thread.
 java.sql.DatabaseMetaData getDatabaseMetaData()
           
protected  SqlConnectionFactory getDefaultConnectionSource()
           
 GrinderResultSet getLastResultSet()
          Gets the LastResultSet attribute of the RDBConnection object
protected  java.lang.Object[] getObjectArrays()
           
 ObjectPoolBroker getObjectPoolBroker()
          Every connection may end up manipulating the ObjectPool.
protected  RDBConnectionPool getPool()
          The associated connection pool of which this connection is a member.
 java.lang.Thread getThread()
          Get the thread that is currently related to this connection.
 void initialize(RDBConnectionPool pool, java.lang.String connectUrl, java.lang.String userName, java.lang.String password)
           
 boolean isInTransaction()
           
 int nextPrimaryKey(SQLSelect sqlA)
           
 void preparedUpdateSql(SQLModifier sqlObj, Persistence pObj)
          Execute java.sql.PreparedStatement code, perhaps creating the PreparedStatement object in the process.
 void rollback()
          Issues a database rollback.
 void setBrokerProperty(BrokerPropertyIF b)
          The ability to support properties other than System.getProperties();
protected  void setConnection(java.sql.Connection newValue)
          Change the connection to the new value.
 void setConnectionSource(SqlConnectionFactory factory)
          Allow brokers to change the connection factory.
 void setObjectPoolBroker(ObjectPoolBroker broker)
          If the framework is using an object pool in conjunction with the database, we need to support transactional access to that object pool.
protected  void setThread(java.lang.Thread t)
           
protected  boolean tryAgain(int errorCode)
          There may be error codes that mean the connection should simply try the statement a second time.
 void updateExecSql(SQLAssembler sqlObj)
          Issue an update statement.
protected  void updateStatement(java.sql.PreparedStatement stmt)
          Usually a no op.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sqlStatementCount

public static int sqlStatementCount
Description of the Field


verbProp

protected static final java.lang.String verbProp
See Also:
Constant Field Values

insrtProp

protected static final java.lang.String insrtProp
See Also:
Constant Field Values

metricsProp

protected static final java.lang.String metricsProp
See Also:
Constant Field Values

verbose

protected static boolean verbose

doInsert

protected static boolean doInsert

manageTransaction

protected static boolean manageTransaction
If manageTransaction is false, no commit or rollback calls will ever be made.


metrics

protected static boolean metrics

connectionSource

protected SqlConnectionFactory connectionSource

userName

protected java.lang.String userName

password

protected java.lang.String password

connectUrl

protected java.lang.String connectUrl

connection

protected java.sql.Connection connection

inTransaction

protected boolean inTransaction

supportsTransaction

protected boolean supportsTransaction

needsExplicitBegin

protected boolean needsExplicitBegin

thread

protected java.lang.Thread thread

pool

protected RDBConnectionPool pool

poolBroker

protected ObjectPoolBroker poolBroker

objArrays

protected final java.lang.Object[] objArrays

lastResultSet

protected GrinderResultSet lastResultSet

cachedStatements

protected SqlStatementCacheItem[] cachedStatements

cachedCount

protected int cachedCount

cachedUpdateStmt

protected java.sql.Statement cachedUpdateStmt
Method Detail

setConnectionSource

public void setConnectionSource(SqlConnectionFactory factory)
Allow brokers to change the connection factory.

Parameters:
factory - The new ConnectionSource value

setObjectPoolBroker

public void setObjectPoolBroker(ObjectPoolBroker broker)
If the framework is using an object pool in conjunction with the database, we need to support transactional access to that object pool.

Parameters:
broker - The new ObjectPoolBroker value

setBrokerProperty

public void setBrokerProperty(BrokerPropertyIF b)
The ability to support properties other than System.getProperties();

Parameters:
b - BrokerPropertyIF The source from which to determine broker properties.

setThread

protected void setThread(java.lang.Thread t)
Parameters:
t - java.lang.Thread

setConnection

protected void setConnection(java.sql.Connection newValue)
Change the connection to the new value.

Parameters:
newValue - The new Connection value

getObjectPoolBroker

public ObjectPoolBroker getObjectPoolBroker()
Every connection may end up manipulating the ObjectPool. Since this needs to be done in a transactional manner, the actual access to the pool is done through the ObjectPoolBroker.

Returns:
com.objectwave.persist.ObjectPoolBroker The associated object pool broker.

getDatabaseMetaData

public java.sql.DatabaseMetaData getDatabaseMetaData()
                                              throws java.sql.SQLException
Returns:
java.sql.DatabaseMetaData
Throws:
java.sql.SQLException - Description of Exception

getThread

public java.lang.Thread getThread()
Get the thread that is currently related to this connection.

Returns:
Thread Null if there is not related thread.

getLastResultSet

public GrinderResultSet getLastResultSet()
Gets the LastResultSet attribute of the RDBConnection object

Returns:
The LastResultSet value

isInTransaction

public boolean isInTransaction()
Returns:
The InTransaction value

getDefaultConnectionSource

protected SqlConnectionFactory getDefaultConnectionSource()
Returns:
The DefaultConnectionSource value

getObjectArrays

protected java.lang.Object[] getObjectArrays()
Returns:
java.lang.Object[]

getPool

protected RDBConnectionPool getPool()
The associated connection pool of which this connection is a member.

Returns:
com.objectwave.persist.RDBConnectionPool

beginTransaction

public void beginTransaction()
                      throws java.sql.SQLException
If the database supports transactions this method would begin the transaction.

Throws:
java.sql.SQLException - Description of Exception

commit

public void commit()
            throws java.sql.SQLException
Issues a database Commit.

Throws:
java.sql.SQLException - Description of Exception

execSql

public void execSql(java.lang.String aString)
             throws java.sql.SQLException
Execute and finish. Dangerous. Don't use. Exposed for bad reasons.

Parameters:
aString - Description of Parameter
Throws:
java.sql.SQLException - Description of Exception

executeQuery

public java.util.List executeQuery(java.lang.String aString,
                                   int numberOfColumns)
                            throws java.sql.SQLException
Execute and finish. Dangerous. Don't use. Exposed for bad reasons.

Parameters:
aString - Description of Parameter
numberOfColumns -
Returns:
Throws:
java.sql.SQLException - Description of Exception

rollback

public void rollback()
              throws java.sql.SQLException
Issues a database rollback.

Throws:
java.sql.SQLException - Description of Exception

freeConnection

public void freeConnection()
Every connection is associated with a particular thread. This drops the relationship between the connection and the thread. Once the pool has been notified that the connection is free, this connection is free to be used by an additional thread.

See Also:
com.objectwave.persist.RDBConnectionPool

alterVendorConnection

public void alterVendorConnection(java.sql.Connection con)
                           throws java.sql.SQLException
Callback hook from connectionSource to allow vendor specific modification to the connection object.

Parameters:
con -
Throws:
java.sql.SQLException

initialize

public void initialize(RDBConnectionPool pool,
                       java.lang.String connectUrl,
                       java.lang.String userName,
                       java.lang.String password)
Parameters:
pool -
connectUrl -
userName -
password -

clearConnection

protected void clearConnection()
Only in the event of a very bad thing should this method be invoked. This will attempt to clean up the database connection related attributes of this object.


addStatement

protected void addStatement(SQLModifier modifier,
                            java.lang.Class persistenceClass,
                            java.sql.PreparedStatement stmt)
Add a prepared statement to this connection's path.

Parameters:
modifier - com.objectwave.persist.SQLModifier
stmt - java.sql.PreparedStatement
persistenceClass - The feature to be added to the Statement attribute

finalize

protected void finalize()
                 throws java.sql.SQLException
Disconnect from database if not already done.

Overrides:
finalize in class java.lang.Object
Throws:
java.sql.SQLException - Description of Exception

findExecSql

public GrinderResultSet findExecSql(SQLAssembler sqlObj)
                             throws java.sql.SQLException,
                                    QueryException
Used for executing any sql query statement.

Parameters:
sqlObj - Description of Parameter
Returns:
Description of the Returned Value
Throws:
java.sql.SQLException - Description of Exception
QueryException - Description of Exception

findExecSql

public GrinderResultSet findExecSql(SQLAssembler sqlObj,
                                    boolean release)
                             throws java.sql.SQLException,
                                    QueryException
Used for executing any sql query statement.

Parameters:
sqlObj - Description of Parameter
release - Description of Parameter
Returns:
Description of the Returned Value
Throws:
java.sql.SQLException - Description of Exception
QueryException - Description of Exception

preparedUpdateSql

public void preparedUpdateSql(SQLModifier sqlObj,
                              Persistence pObj)
                       throws java.sql.SQLException,
                              QueryException
Execute java.sql.PreparedStatement code, perhaps creating the PreparedStatement object in the process.

Parameters:
sqlObj - Description of Parameter
pObj - Description of Parameter
Throws:
java.sql.SQLException - Description of Exception
QueryException - Description of Exception

updateExecSql

public void updateExecSql(SQLAssembler sqlObj)
                   throws QueryException,
                          java.sql.SQLException
Issue an update statement.

Parameters:
sqlObj - Description of Parameter
Throws:
QueryException - Description of Exception
java.sql.SQLException - Description of Exception

tryAgain

protected boolean tryAgain(int errorCode)
There may be error codes that mean the connection should simply try the statement a second time.

Parameters:
errorCode - Description of Parameter
Returns:
true if the sql statement should be tried a second time.

updateStatement

protected void updateStatement(java.sql.PreparedStatement stmt)
Usually a no op. This is a hook point for extending connections.

Parameters:
stmt - java.sql.PreparedStatement

nextPrimaryKey

public int nextPrimaryKey(SQLSelect sqlA)
                   throws java.sql.SQLException,
                          QueryException
Parameters:
sqlA - Description of Parameter
Returns:
Description of the Returned Value
Throws:
java.sql.SQLException - Description of Exception
QueryException - Description of Exception

checkConnection

protected java.sql.Connection checkConnection()
                                       throws java.sql.SQLException
Verify that there is already a connection to the relational database. If not, establish one. There should only be 1 thread per connection.

Returns:
Throws:
java.sql.SQLException - Description of Exception