com.triactive.jdo.store
Class RetryableTransaction

java.lang.Object
  extended bycom.triactive.jdo.store.RetryableTransaction
Direct Known Subclasses:
StoreManager.MgmtTransaction

public abstract class RetryableTransaction
extends java.lang.Object

An abstract base class for JDBC transactions that can be retried in the face of transient failures. Subclasses must override, among others, the execute(int) method to implement the body of the transaction. Users call the execute() method to execute the transaction.

Author:
Mike Martin

Field Summary
protected  java.sql.Connection conn
           
 
Constructor Summary
RetryableTransaction(int isolationLevel)
          Constructs a new transaction having the given isolation level.
RetryableTransaction(int isolationLevel, int maxRetries)
          Constructs a new transaction having the given isolation level and number of retries.
 
Method Summary
 java.lang.Object execute()
          Executes the transaction.
protected abstract  java.lang.Object execute(int attempts)
          Implements the body of the transaction.
 void executeStatements(java.util.Collection stmts)
          Convenience method for executing a collection of SQL statements during the transaction.
 java.sql.Connection getConnection()
          Returns the JDBC connection currently being used by the transaction.
protected abstract  InitializedConnectionFactory getConnectionFactory()
          Returns the connection factory from which this object obtains its JDBC connections.
abstract  DatabaseAdapter getDatabaseAdapter()
          Returns the database adapter associated with connections provided by this object's connection factory.
abstract  java.lang.String toString()
          Returns a description of the transaction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

conn

protected java.sql.Connection conn
Constructor Detail

RetryableTransaction

public RetryableTransaction(int isolationLevel)
Constructs a new transaction having the given isolation level. The maximum number of retries is taken from the system property com.triactive.jdo.store.maxRetries, or defaults to 3 if not set.

Parameters:
isolationLevel - One of the isolation level constants from java.sql.Connection.

RetryableTransaction

public RetryableTransaction(int isolationLevel,
                            int maxRetries)
Constructs a new transaction having the given isolation level and number of retries.

Parameters:
isolationLevel - One of the isolation level constants from java.sql.Connection.
maxRetries - The maximum number of times the transaction will be tried. The value includes the first attempt.
Method Detail

getConnectionFactory

protected abstract InitializedConnectionFactory getConnectionFactory()
Returns the connection factory from which this object obtains its JDBC connections. Subclasses override this method to provide a specific factory.


getDatabaseAdapter

public abstract DatabaseAdapter getDatabaseAdapter()
Returns the database adapter associated with connections provided by this object's connection factory. Subclasses override this method to provide a specific adapter.


execute

public final java.lang.Object execute()
Executes the transaction.

A database connection is acquired and the execute(int) method is invoked. If the selected isolation level is not Connection.TRANSACTION_NONE, then commit() or rollback() is called on the connection according to whether the invocation succeeded or threw an exception.

If the invocation fails due to a SQLException, and the failure is considered worth retrying, the transaction is repeated up to the specified number of retries. "Worth retrying" is defined by the exception's SQLSTATE code. If the driver doesn't provide SQLSTATE codes then all exceptions are considered worth retrying.

Returns:
The value returned by the inner execute method.
Throws:
javax.jdo.JDODataStoreException - If a SQL exception occurred even after retrying.
See Also:
SqlState.isWorthRetrying()

getConnection

public final java.sql.Connection getConnection()
Returns the JDBC connection currently being used by the transaction.

Throws:
java.lang.IllegalStateException - if the transaction object is not in the process of being executed

execute

protected abstract java.lang.Object execute(int attempts)
                                     throws java.sql.SQLException
Implements the body of the transaction. A connection to the database has been acquired and saved in the conn field. If the selected isolation level is Connection.TRANSACTION_NONE the connection has auto-commit set to true, otherwise it's false.

Parameters:
attempts - The number of times this transaction has already been attempted. If greater than zero then this is a retry.
Throws:
java.sql.SQLException - If the transaction fails due to a database error that might allow the entire transaction to be retried.

executeStatements

public void executeStatements(java.util.Collection stmts)
                       throws java.sql.SQLException
Convenience method for executing a collection of SQL statements during the transaction. Typically used to execute DDL since no result is returned.

Parameters:
stmts - A collection of string that are the statements to be executed. The statements are executed in the order returned by the collection's iterator.
Throws:
java.lang.IllegalStateException - if the transaction object is not in the process of being executed
java.sql.SQLException - if a database error occurs

toString

public abstract java.lang.String toString()
Returns a description of the transaction. Subclasses should override this method so that transaction failures are given an appropriate exception message.

Returns:
A description of the transaction.


Copyright 2001-2007 The TJDO Project All Rights Reserved.