com.framework.jdto.transaction
Class TransactionContextManager

java.lang.Object
  extended bycom.framework.jdto.transaction.TransactionContextManager

public class TransactionContextManager
extends Object

TransactionContextManager class is mainly responsible for managing the transactions. it is responsible for begining the transaction and ending the transaction based on the current transaction status.

User needs to call the beginTransaction() method to begin the transaction and should end the transaction by calling the endTransaction() method. All the DAO's which are executed after the beginTransaction() method and before the endTransaction() method by default runs in a transaction, Provided the DAO's use the PreparedStatementWrapper or StatementWrapper classes to interact with the database (i.e., Insert, Update, Delete and Select Queries) instead of the actual PreparedStatement and Statement class of java.sql package.

Author:
msatish

Field Summary
static int CONTEXT_MANAGER
           
 
Method Summary
static Transaction beginTransaction()
          The beginTransaction() method begins the transaction.
static void clear()
          Clear the current transactionContext.
static void commitTransaction()
          This method commits the Transaction provided the current Transaction Status is STATUS_MARKED_COMMIT.
static void endTransaction()
          The endTransaction() method ends the transaction.
static Transaction getCurrentTransaction()
          This method gets the currently running Transaction
static TransactionContextManager getInstance()
          This method ensures there is one and only one TransactionContextManager available to handle all the transactions.
static void rollbackTransaction()
          This method rollback's the Transaction provided the current Transaction Status is STATUS_MARKED_ROLLBACK.
static void setCurrentTransactionStatus(int status)
          Sets the status of the current transaction.
static void setRollbackOnly()
          This method sets the current Transaction Status as STATUS_MARKED_ROLLBACK, so that the framework rollback's the transaction.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONTEXT_MANAGER

public static final int CONTEXT_MANAGER
See Also:
Constant Field Values
Method Detail

getInstance

public static TransactionContextManager getInstance()
This method ensures there is one and only one TransactionContextManager available to handle all the transactions.

Returns:
ctxManager - TransactionContextManager

beginTransaction

public static Transaction beginTransaction()
                                    throws NotSupportedException
The beginTransaction() method begins the transaction. The transaction will end only when the user calls the endTransation() method. Once this method is called by the user, all the PreparedStatementWrapper or StatementWrapper objects that are called after the call to beginTransaction() method will run bydefault in the same Trasaction. When a transaction is already running and user requests for a nested transaction, this method throws a NotSupportedException. It is left to the user/developer to continue or abort the currently running transaction. If user/developer wants to abort/rollback the currently running transaction, then he should catch the NotSupportedException and call the TransactionContextManager.setRollbackOnly(); with in the Exception block.

Returns:
transaction - current transaction context.
Throws:
NotSupportedException - when a transaction is already running and user requests for a nested transaction.

endTransaction

public static void endTransaction()
The endTransaction() method ends the transaction. This method has to be explicitly called by the user inorder to end the Transaction. The transaction will either be comitted or rolled back based on the current Transaction Status. if the current Transaction Status is STATUS_MARKED_COMMIT then the Transaction will be comitted and if it is STATUS_MARKED_ROLLBACK then the Transaction will be rolled back.

This method rollback's the transaction if any SQLException or DAOException occurs in the DAO Classes, otherwise commits the transaction. The developer doesnt need to worry about setting the current Transaction Status, as this framework takes care of setting the Transaction Status by itself, based on the success or failure of each query exection in the DAO's.

It is always recommended to use this endTransaction() method in the finally block, so as to avoid the ambiguity.

Throws:
SQLException - if database error occurs

setCurrentTransactionStatus

public static void setCurrentTransactionStatus(int status)
Sets the status of the current transaction. User is not required to call this method explicitly, as the framework takes care of setting the current transaction status. This method can also be called by the developer, if he wants to set the Current Transaction Status explicitly for his DAO's.

This method is called by the framework on success or failure of each SQL Query. On successful execution of each SQL Query, the framework sets the current Transaction Status as STATUS_MARKED_COMMIT. if atleast one failure occurs the current Transaction Status is set as STATUS_MARKED_ROLLBACK and the transaction will be rolledback by the framework.


getCurrentTransaction

public static Transaction getCurrentTransaction()
This method gets the currently running Transaction

Returns:
Current Transaction. Null if there is no Transaction Running.

setRollbackOnly

public static void setRollbackOnly()
This method sets the current Transaction Status as STATUS_MARKED_ROLLBACK, so that the framework rollback's the transaction.


commitTransaction

public static void commitTransaction()
                              throws SQLException
This method commits the Transaction provided the current Transaction Status is STATUS_MARKED_COMMIT. Developer is not required to call this method, if the endTransaction() method is called. The framework takes care of commiting or rollingback the Transaction in this case. if the developer wants to manage the transaction on his own he can call commitTransaction() and rollbackTransaction() methods explicitly. The developer doesnt need to worry about setting the current Transaction Status, as this framework takes care of setting the Transaction Status by itself, based on the success or failure of each query exection in the DAO's.

Throws:
SQLException

rollbackTransaction

public static void rollbackTransaction()
                                throws SQLException
This method rollback's the Transaction provided the current Transaction Status is STATUS_MARKED_ROLLBACK. The developer doesnt need to worry about setting the current Transaction Status, as this framework takes care of setting the Transaction Status by itself, based on the success or failure of each query exection in the DAO's.

Throws:
SQLException

clear

public static void clear()
Clear the current transactionContext.