tppt.xopen
Class ResourceManager

java.lang.Object
  |
  +--tppt.xopen.ResourceManager
All Implemented Interfaces:
XA

public class ResourceManager
extends java.lang.Object
implements XA

ResourceManager acts as proxy for the DBMS. For each connection to the DBMS, we create an instance of ResourceManager to handle the database activities. It implements the XA interface used to interact with Transaction Mangaer.

Version:
2.0
Author:
Jodie Wu
See Also:
XA, AX, TwoPhaseLogger, DBManager, DBSetting

Field Summary
private static java.util.ArrayList abortList
          The abortList stored a list of should-be-aborting ResourceManager object.
protected static AX ax
          The AX object.
protected  DBManager db
          This object is responsbile to interface with the DBMS.
private  TwoPhaseLogger logger
          The log object.
private  java.util.Random random
          The Random object using the time as the seed.
protected  int rid
          ResouceManager id
protected  DBSetting setting
          Database Setting object used to stored the JDBC setting parameters.
private  int status
          store the random generated status for this RM.
protected  int tid
          Global Transaction Id
private static java.util.ArrayList trace
          The trace list stores a list of created ResourceManager object.
 
Constructor Summary
ResourceManager(int tid, int rid, DBSetting s)
          The constructor.
 
Method Summary
static ResourceManager connectToRM(int tid, int rid, DBSetting s)
          This method has to be called by ApplicationProgram when setting up its subtransaction.
 boolean equals(java.lang.Object o)
          Overwrite the equals method so that 2 RMs are the same iff both their tid and rid are the same.
 boolean executeTransaction(java.lang.String sql)
          This method has to be called by user's ApplicationProgram.
 void forceCloseConnection()
          Force this resource manager to release the connection and db lock.
(package private) static java.util.ArrayList getAbortList()
          Obtain the abort list.
 int getRMId()
          Get the resource manager id
(package private)  int getStatus()
          Get the status of this resource manager.
(package private) static java.util.ArrayList getTraceList()
          Obtain the trace list.
 int getTransactionId()
          Return the transaction id that this resource manager belong to.
 boolean registerToTM()
          This method must be called by the ApplicationProgram so that it will register itself to the Transaction Manager.
private  boolean setupConnection()
          Create the DBManager object if it has not been created.
static void setXA(AX a)
          Set up the AX.
 boolean startTransaction()
          This method has to be called by user's Application Program so that the resource manager could start setup the connection and then start the local transaction to the underlining DBMS.
 boolean xa_close()
          XA implementation.
 boolean xa_commit()
          XA implementation.
 int xa_prepare()
          The XA implementation.
 boolean xa_rollback()
          XA implementation.
 
Methods inherited from class java.lang.Object
, clone, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

rid

protected int rid
ResouceManager id

tid

protected int tid
Global Transaction Id

logger

private TwoPhaseLogger logger
The log object.

setting

protected DBSetting setting
Database Setting object used to stored the JDBC setting parameters.
See Also:
DBSetting

ax

protected static AX ax
The AX object. It is set when the TransactionManager class is loaded in the runtime. The TransactionManager will first cast to AX, and then stored in the this variable. Therefore, it is actually the TM. It is worth to note that we should use the Interface object instead of TransactionManager class directly because we know nothing about it before we actually loaded it in the runtime. Also this varaible is static because we only need one Transaction Manager in the simulation.

db

protected DBManager db
This object is responsbile to interface with the DBMS.

random

private java.util.Random random
The Random object using the time as the seed.

status

private int status
store the random generated status for this RM. For tracking usage.

trace

private static java.util.ArrayList trace
The trace list stores a list of created ResourceManager object. For tracking usage.

abortList

private static java.util.ArrayList abortList
The abortList stored a list of should-be-aborting ResourceManager object. For Tracking usage.
Constructor Detail

ResourceManager

public ResourceManager(int tid,
                       int rid,
                       DBSetting s)
The constructor. It constructs the ResourceManager instance with tid,rid and DBSetting object. It also obtains the logger instance.
Parameters:
tid - transaction id.
rid - resource manager id
s - DBSetting object
Method Detail

setXA

public static void setXA(AX a)
Set up the AX.
Parameters:
a - The AX object

getAbortList

static java.util.ArrayList getAbortList()
Obtain the abort list.
Returns:
the abort list

getTraceList

static java.util.ArrayList getTraceList()
Obtain the trace list.
Returns:
the trace list.

connectToRM

public static ResourceManager connectToRM(int tid,
                                          int rid,
                                          DBSetting s)
This method has to be called by ApplicationProgram when setting up its subtransaction. For each subTransaction, you should create its own ResourceManager object and then control obtained ResourceManager instace in order to register the subtransaction to TM, start the sub-transaction and execute the sub-transaction.
Parameters:
tid - the transaction Id
rid - this resource manager id
s - JDBC setting
Returns:
return newly created ResourceManager instace.

getRMId

public int getRMId()
Get the resource manager id
Returns:
return the id of this resource manager.

getTransactionId

public int getTransactionId()
Return the transaction id that this resource manager belong to.
Returns:
return transaction id

registerToTM

public boolean registerToTM()
This method must be called by the ApplicationProgram so that it will register itself to the Transaction Manager.
Returns:
true if register properly, false otherwise.
See Also:
AX.ax_reg(int, tppt.xopen.ResourceManager)

setupConnection

private boolean setupConnection()
Create the DBManager object if it has not been created. And then connect to the specified DBMS. This method is called by startTransaction method.
Returns:
true if the connection created sucessfully; false otherwise.
See Also:
DBManager, DBSetting, DBManager.createConnection(DBSetting)

startTransaction

public boolean startTransaction()
This method has to be called by user's Application Program so that the resource manager could start setup the connection and then start the local transaction to the underlining DBMS.
Returns:
true if transaction has been started properly; false otherwise;
See Also:
setupConnection(), DBManager.startTransaction()

executeTransaction

public boolean executeTransaction(java.lang.String sql)
This method has to be called by user's ApplicationProgram. It will ask the DBManager to execute the SQL statement.
Parameters:
sql - the SQL command
Returns:
true if the transaction executes properly; false otherwise.

xa_prepare

public int xa_prepare()
               throws PreparedTimeoutException
The XA implementation. This method should be called by the Transaction Manager in the first phase of the two-phase commit protocol. This method will randomly return the vote. Right now, 70% it will vote TM_VOTE_READY, 20% it will vote TM_VOTE_ABORT and 10% it will throws the PreparedTimeoutException.
Specified by:
xa_prepare in interface XA
Returns:
TM_VOTE_READY: to vote ready, TM_VOTE_ABORT: vote abort
Throws:
PreparedTimeoutException - if this resource manager is timeout.

xa_rollback

public boolean xa_rollback()
XA implementation. This method should be called by TransationManager when one of cohorts votes abort. It rollbacks the local transaction.
Specified by:
xa_rollback in interface XA
Returns:
return true if rollback sucessfully, false otherwise.
See Also:
DBManager.rollbackTransaction()

xa_close

public boolean xa_close()
XA implementation. This method should be called by TransationManager in the end of the two-phase commit protocol. It releases the connection and db lock for this transaction.
Specified by:
xa_close in interface XA
Returns:
true if the transaction closes sucessfully, false otherwise.
See Also:
DBManager.closeTransaction()

xa_commit

public boolean xa_commit()
XA implementation. This method should be called by TransationManager when all of the cohorts votes READY. It commits the local transaction.
Specified by:
xa_commit in interface XA
Returns:
true if commit sucessfully, false otherwise.
See Also:
DBManager.commitTransaction()

equals

public boolean equals(java.lang.Object o)
Overwrite the equals method so that 2 RMs are the same iff both their tid and rid are the same.
Overrides:
equals in class java.lang.Object
Parameters:
o - the ResourceManaher object
Returns:
true if two RM are the same, false otherwise.

getStatus

int getStatus()
Get the status of this resource manager. For tracking usage.
Returns:
the status of the Resource Manager.
         Message.RM_STATUS_WAIT: If no action yet
         Message.RM_STATUS_COMMIT: commited
         Message.RM_STATUS_ABORT: abort
  

forceCloseConnection

public void forceCloseConnection()
Force this resource manager to release the connection and db lock.