net.sf.farrago.catalog
Class FarragoReposTxnContext

java.lang.Object
  extended by net.sf.farrago.catalog.FarragoReposTxnContext
Direct Known Subclasses:
MockReposTxnContext

public class FarragoReposTxnContext
extends Object

FarragoReposTxnContext manages the state of at most one repository transaction. A context may be inactive, meaning it has no current transaction.

Always use the following exception-safe transaction pattern:


   FarragoReposTxnContext txn = repos.newTxnContext();
   try {
       txn.beginWriteTxn();
       ... do stuff which accesses repository ...
       txn.commit();
   } finally {
       // no effect if already committed or beginWriteTxn failed
       txn.rollback();
   }

Version:
$Id: //open/dev/farrago/src/net/sf/farrago/catalog/FarragoReposTxnContext.java#19 $
Author:
John V. Sichi

Nested Class Summary
private static class FarragoReposTxnContext.State
           
 
Field Summary
private  int lockLevel
           
private  boolean manageReposSession
           
private  boolean readOnly
           
private  FarragoRepos repos
           
private  FarragoReposTxnContext.State state
           
 
Constructor Summary
FarragoReposTxnContext(FarragoRepos repos)
          Creates a new inactive transaction context with manual repository session management.
FarragoReposTxnContext(FarragoRepos repos, boolean manageRepoSession)
          Creates a new inactive transaction context.
FarragoReposTxnContext(FarragoRepos repos, boolean manageRepoSession, boolean readOnly)
          Creates a new inactive transaction context that's optionally readonly.
 
Method Summary
 void beginExclusiveAccess()
          Puts the repository in exclusive access mode.
 void beginLockedTxn(boolean readOnly)
          Acquires a repository lock and begins a matching MDR transaction (shared lock for read, or exclusive lock for write).
 void beginReadTxn()
          Begins a new read-only transaction.
 void beginWriteTxn()
          Begins a new read/write transaction.
 void commit()
          Commits the active transaction, if any.
 void endExclusiveAccess()
          Ends exclusive access mode for the repository.
 boolean isReadTxnInProgress()
           
 boolean isTxnInProgress()
           
 void rollback()
          Rolls back the active transaction, if any.
 void setReposReadOnly()
          Switches the repository to read-only mode.
 void unlockAfterTxn()
          Releases lock acquired by beginLockedTxn.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

repos

private FarragoRepos repos

state

private FarragoReposTxnContext.State state

lockLevel

private int lockLevel

manageReposSession

private final boolean manageReposSession

readOnly

private boolean readOnly
Constructor Detail

FarragoReposTxnContext

public FarragoReposTxnContext(FarragoRepos repos)
Creates a new inactive transaction context with manual repository session management.

Parameters:
repos - the repos against which transactions are to be performed

FarragoReposTxnContext

public FarragoReposTxnContext(FarragoRepos repos,
                              boolean manageRepoSession)
Creates a new inactive transaction context.

Parameters:
repos - the repos against which transactions are to be performed
manageRepoSession - if true, a repository session is wrapped around each transaction

FarragoReposTxnContext

public FarragoReposTxnContext(FarragoRepos repos,
                              boolean manageRepoSession,
                              boolean readOnly)
Creates a new inactive transaction context that's optionally readonly.

Parameters:
repos - the repos against which transactions are to be performed
manageRepoSession - if true, a repository session is wrapped around each transaction
Method Detail

isTxnInProgress

public boolean isTxnInProgress()
Returns:
whether a transaction is currently in progress

isReadTxnInProgress

public boolean isReadTxnInProgress()
Returns:
whether a read-only transaction is currently in progress

beginReadTxn

public void beginReadTxn()
Begins a new read-only transaction.


beginWriteTxn

public void beginWriteTxn()
Begins a new read/write transaction.


commit

public void commit()
Commits the active transaction, if any.


rollback

public void rollback()
Rolls back the active transaction, if any.


beginLockedTxn

public void beginLockedTxn(boolean readOnly)
Acquires a repository lock and begins a matching MDR transaction (shared lock for read, or exclusive lock for write). Typical usage is start of SQL statement preparation (e.g. readOnly=true for DML or query, false for DDL).

Parameters:
readOnly - if true, a shared lock is acquired on the catalog; otherwise, an exclusive lock is acquired

unlockAfterTxn

public void unlockAfterTxn()
Releases lock acquired by beginLockedTxn. Caller should already have ended transaction with either commit or rollback.


beginExclusiveAccess

public void beginExclusiveAccess()
Puts the repository in exclusive access mode. When in this mode, subsequent attempts to lock the repository will return an exception immediately rather than wait for a required repository lock to become available.


endExclusiveAccess

public void endExclusiveAccess()
Ends exclusive access mode for the repository.


setReposReadOnly

public void setReposReadOnly()
Switches the repository to read-only mode.