jodd.jtx
Class JtxTransaction

java.lang.Object
  extended by jodd.jtx.JtxTransaction
Direct Known Subclasses:
DbJtxTransaction

public class JtxTransaction
extends java.lang.Object

Transaction is an unit of work that is performed by one or more resources. Created and controlled by transaction manager.

Transaction is associated to a thread from where it was created. Transaction may have more than one resource attached.

Committing and rolling back transaction actually performs commit/rollback on all attached resources. Therefore, it may happens that committing of one resource fails, what actually breaks the atomicity of the transaction. If transaction is attached just to one resource, atomicity is saved.

Only one resource of some type may exists in the transaction. There is no way to have two resources of the same type in one transaction.

This class is responsible for transaction life-cycle. It can be consider that all work is scoped to associated thread.


Field Summary
protected  long deadline
           
protected  JtxTransactionMode mode
           
protected  java.util.Set<jodd.jtx.JtxResource> resources
           
protected  java.lang.Throwable rollbackCause
           
protected  JtxStatus status
           
protected  JtxTransactionManager txManager
           
 
Constructor Summary
JtxTransaction(JtxTransactionManager txManager, JtxTransactionMode mode)
          Creates new transaction.
 
Method Summary
protected  void checkTimeout()
          Sets the rollback-only if the deadline has been reached and throws an exception.
 void commit()
          Commit and completes current transaction.
protected  void commitAllResources()
          Commits all attached resources.
protected  void commitOrRollback(boolean doCommit)
          Performs either commit or rollback on all transaction resources.
 JtxStatus getStatus()
          Returns current transaction status.
 JtxTransactionManager getTransactionManager()
          Returns transaction manager that owns this transaction.
 JtxTransactionMode getTransactionMode()
          Returns transaction mode.
 boolean isActive()
          Returns true if transaction is active.
 boolean isCommitted()
          Returns true if transaction and all its resources are committed successfully.
 boolean isCompleted()
          Returns true if transaction is either committed or rolled back.
 boolean isNoTransaction()
          Returns true if transaction is explicitly forbidden, i.e.
 boolean isRollbackOnly()
          Returns true if transaction is marked as rollback only.
 boolean isRolledback()
          Returns true if transaction and all its resources are rolled-back successfully.
protected
<E> E
lookupResource(java.lang.Class<E> resourceType)
          Lookups for open resource.
<E> E
requestResource(java.lang.Class<E> resourceType)
          Requests a resource.
protected  void rolbackAllResources(boolean wasForced)
          Rollbacks all attached resources.
 void rollback()
          Roll back and completes current transaction.
 void setRollbackOnly()
          Modify the transaction associated with the target object such that the only possible outcome of the transaction is to roll back the transaction.
 void setRollbackOnly(java.lang.Throwable th)
          Modify the transaction associated with the target object such that the only possible outcome of the transaction is to roll back the transaction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

txManager

protected final JtxTransactionManager txManager

mode

protected final JtxTransactionMode mode

resources

protected final java.util.Set<jodd.jtx.JtxResource> resources

deadline

protected final long deadline

status

protected JtxStatus status

rollbackCause

protected java.lang.Throwable rollbackCause
Constructor Detail

JtxTransaction

public JtxTransaction(JtxTransactionManager txManager,
                      JtxTransactionMode mode)
Creates new transaction. Should be invoked by JtxTransactionManager.

Method Detail

getTransactionMode

public JtxTransactionMode getTransactionMode()
Returns transaction mode.


getTransactionManager

public JtxTransactionManager getTransactionManager()
Returns transaction manager that owns this transaction.


getStatus

public JtxStatus getStatus()
Returns current transaction status.


isActive

public boolean isActive()
Returns true if transaction is active.


isNoTransaction

public boolean isNoTransaction()
Returns true if transaction is explicitly forbidden, i.e. session is in auto-commit mode, or not started yet.


isCommitted

public boolean isCommitted()
Returns true if transaction and all its resources are committed successfully.


isRolledback

public boolean isRolledback()
Returns true if transaction and all its resources are rolled-back successfully.


isCompleted

public boolean isCompleted()
Returns true if transaction is either committed or rolled back.


setRollbackOnly

public void setRollbackOnly()
Modify the transaction associated with the target object such that the only possible outcome of the transaction is to roll back the transaction.


setRollbackOnly

public void setRollbackOnly(java.lang.Throwable th)
Modify the transaction associated with the target object such that the only possible outcome of the transaction is to roll back the transaction.


isRollbackOnly

public boolean isRollbackOnly()
Returns true if transaction is marked as rollback only.


checkTimeout

protected void checkTimeout()
Sets the rollback-only if the deadline has been reached and throws an exception.


commit

public void commit()
Commit and completes current transaction. Transaction is committed on all attached resources. After, resources are detached from the transaction. When this method completes,transaction is no longer associated with current thread.


rollback

public void rollback()
Roll back and completes current transaction. Transaction is rolled back on all attached resources. Resource are then detached from the transaction. When this method completes, transaction is no longer associated with current thread.


commitOrRollback

protected void commitOrRollback(boolean doCommit)
Performs either commit or rollback on all transaction resources.


commitAllResources

protected void commitAllResources()
                           throws JtxException
Commits all attached resources. On successful commit, resource will be closed and detached from this transaction. On exception, resource remains attached to transaction.

All resources will be committed, even if commit fails on some in that process. If there was at least one failed commit, its exception will be re-thrown after finishing committing all resources, and transaction will be marked as rollback only.

Throws:
JtxException

rolbackAllResources

protected void rolbackAllResources(boolean wasForced)
Rollbacks all attached resources. Resource will be closed. and detached from this transaction. If exception occurs, it will be rethrown at the end.


requestResource

public <E> E requestResource(java.lang.Class<E> resourceType)
Requests a resource. If resource is not found, it will be created and new transaction will be started on it.


lookupResource

protected <E> E lookupResource(java.lang.Class<E> resourceType)
Lookups for open resource. Returns null if resource not found. Only open resources can be found.



Copyright ©2008 Jodd Team