org.hypergraphdb.transaction
Class HGTransactionManager

java.lang.Object
  extended by org.hypergraphdb.transaction.HGTransactionManager

public class HGTransactionManager
extends java.lang.Object

The HGTransactionManager handles transactional activity for a single HyperGraph instance. You can obtain the transaction manager for a HyperGraph by calling its getTransactionManager method.

Author:
Borislav Iordanov

Field Summary
 TxMonitor txMonitor
           
 
Constructor Summary
HGTransactionManager(HGTransactionFactory factory)
           Construct a new transaction manager with the given transaction factory.
 
Method Summary
 void abort()
           Abort the current transaction by calling endTransaction(false).
 void beginTransaction()
           Begin a new transaction in the current transaction context.
 void commit()
           Commit the current transaction by calling endTransaction(true).
 HGTransaction createTransaction(HGTransaction parent)
           Create and return a child transaction of the given parent transaction.
 void disable()
          Disable transactions - equivalent to setEnabled(false)
 void enable()
          Enable transactions - equivalent to setEnabled(true).
 void endTransaction(boolean success)
           Terminate the currently active transaction.
<V> V
ensureTransaction(java.util.concurrent.Callable<V> transaction)
           Perform a unit of work encapsulated as a transaction and return the result.
 HGTransactionContext getContext()
          Return the HGTransactionContext instance associated with the current thread.
 boolean isEnabled()
          Return true if the transaction are enabled and false otherwise.
 void setEnabled(boolean enabled)
          Enable or disable transaction.
 void threadAttach(HGTransactionContext tContext)
           Attach the given transaction context to the current thread.
 void threadDetach()
           Detach the transaction context bound to the current thread.
<V> V
transact(java.util.concurrent.Callable<V> transaction)
           Perform a unit of work encapsulated as a transaction and return the result.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

txMonitor

public TxMonitor txMonitor
Constructor Detail

HGTransactionManager

public HGTransactionManager(HGTransactionFactory factory)

Construct a new transaction manager with the given transaction factory. This method is normally called only internally. To obtain the transactio manager bound to a HyperGraph, use HyperGraph.getTransactionManager.

Parameters:
factory - The HGTransactionFactory responsible for fabricating new transactions.
Method Detail

isEnabled

public boolean isEnabled()

Return true if the transaction are enabled and false otherwise.


setEnabled

public void setEnabled(boolean enabled)

Enable or disable transaction. Note that all current transactions will be silently aborted so make sure any pending transactions are completed before calling this method.

Parameters:
enabled - true if transaction must be henceforth enabled and false otherwise.

enable

public void enable()

Enable transactions - equivalent to setEnabled(true).


disable

public void disable()

Disable transactions - equivalent to setEnabled(false)


getContext

public HGTransactionContext getContext()

Return the HGTransactionContext instance associated with the current thread.


threadAttach

public void threadAttach(HGTransactionContext tContext)

Attach the given transaction context to the current thread. This method is normally called in a server environment. By default, a transaction context will be created and bound to a thread if need be, every time a new transaction is requested. So when HyperGraph is embedded in a client application, there is no need to explicitly attach/detach contexts to threads. However, in an environment using thread pooling such as is common in servers where a single transaction can span multiple requests, use the threadAttach and threadDetach methods to switch transactions contexts bound to clients.

Parameters:
tContext -

threadDetach

public void threadDetach()

Detach the transaction context bound to the current thread. This method is normally called in a server environment. By default, a transaction context will be created and bound to a thread if need be, every time a new transaction is requested. So when HyperGraph is embedded in a client application, there is no need to explicitly attach/detach contexts to threads.

IMPORTANT NOTE: when managing transaction contexts explicitly, you are responsible for closing all pending transactions in the context before disposing of it. This is done by invoking the HGTransactionContext.endAll method.

Parameters:
tContext -

createTransaction

public HGTransaction createTransaction(HGTransaction parent)

Create and return a child transaction of the given parent transaction.

Parameters:
The - parent HGTransaction - if null, a top-level transaction object is returned.
Returns:
The newly created transaction.

beginTransaction

public void beginTransaction()

Begin a new transaction in the current transaction context. If there's no transaction context bound to the active thread, one will be created.


endTransaction

public void endTransaction(boolean success)
                    throws HGTransactionException

Terminate the currently active transaction. The transaction will be aborted or committed based on the success flag (abort when false and commit when true).

You are graced with a HGException if there's no currently active transaction.

Parameters:
success -
Throws:
HGTransactionException

commit

public void commit()

Commit the current transaction by calling endTransaction(true). Wrap the possible HGTransactionException in a HGException.


abort

public void abort()

Abort the current transaction by calling endTransaction(false). Wrap the possible HGTransactionException in a HGException.


ensureTransaction

public <V> V ensureTransaction(java.util.concurrent.Callable<V> transaction)

Perform a unit of work encapsulated as a transaction and return the result. This method will reuse the currently active transaction if there is one or create a new transaction otherwise.

Type Parameters:
V - The type of the return value.
Parameters:
transaction - The transaction process encapsulated as a Callable instance.
Returns:
The result of transaction.call().
Throws:
The - method will (re)throw any exception that does not result from a deadlock.

transact

public <V> V transact(java.util.concurrent.Callable<V> transaction)

Perform a unit of work encapsulated as a transaction and return the result. This method explicitly allows deadlock to occur and it will reattempt the transaction in such a case indefinitely. In order for the transaction to eventually complete, the underlying transactional system must be configured to be fair or to prioritize transaction randomly (which is the default behavior).

Type Parameters:
V - The type of the return value.
Parameters:
transaction - The transaction process encapsulated as a Callable instance.
Returns:
The result of transaction.call().
Throws:
The - method will (re)throw any exception that does not result from a deadlock.