public class JtxTransaction
extends java.lang.Object
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.
Modifier and Type | Field and Description |
---|---|
protected long |
deadline |
protected JtxTransactionMode |
mode |
protected java.util.Set<jodd.jtx.JtxResource> |
resources |
protected java.lang.Throwable |
rollbackCause |
protected java.lang.Object |
scope |
protected boolean |
startAsActive |
protected JtxStatus |
status |
protected JtxTransactionManager |
txManager |
Constructor and Description |
---|
JtxTransaction(JtxTransactionManager txManager,
JtxTransactionMode mode,
java.lang.Object scope,
boolean active)
Creates new transaction.
|
Modifier and Type | Method and Description |
---|---|
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.
|
java.lang.Object |
getScope()
Returns transaction scope if exist, or
null . |
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. |
boolean |
isStartAsActive()
Returns
true if transaction started as active one. |
protected <E> E |
lookupResource(java.lang.Class<E> resourceType)
Lookups for open resource.
|
<E> E |
requestResource(java.lang.Class<E> resourceType)
Requests a resource.
|
void |
rollback()
Roll back and completes current transaction.
|
protected void |
rollbackAllResources(boolean wasForced)
Rollbacks all attached resources.
|
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.
|
protected final JtxTransactionManager txManager
protected final JtxTransactionMode mode
protected final java.util.Set<jodd.jtx.JtxResource> resources
protected final java.lang.Object scope
protected final long deadline
protected final boolean startAsActive
protected java.lang.Throwable rollbackCause
protected JtxStatus status
public JtxTransaction(JtxTransactionManager txManager, JtxTransactionMode mode, java.lang.Object scope, boolean active)
JtxTransactionManager
.
If transaction is set as active
, it will be actually created, meaning
that it is the first transaction on this connection i.e. in this session.
If transaction is not active
, transaction object will be created,
but the real transaction not, and it is expected that one is already created before.txManager
- jtx managermode
- transaction modescope
- transaction live scope within the other transaction requests are ignoredactive
- if true
it is an active transaction, otherwise it's notpublic JtxTransactionMode getTransactionMode()
public JtxTransactionManager getTransactionManager()
public java.lang.Object getScope()
null
.public JtxStatus getStatus()
public boolean isStartAsActive()
true
if transaction started as active one.
This value is never changed, while status
changes during the execution.public boolean isActive()
true
if transaction is active.
This status changes during the transaction flow.public boolean isNoTransaction()
true
if transaction is explicitly forbidden, i.e.
session is in auto-commit mode.public boolean isCommitted()
true
if transaction and all its resources are committed successfully.public boolean isRolledback()
true
if transaction and all its resources are rolled-back successfully.public boolean isCompleted()
true
if transaction is either committed or rolled back.public void setRollbackOnly()
public void setRollbackOnly(java.lang.Throwable th)
public boolean isRollbackOnly()
true
if transaction is marked as rollback only.protected void checkTimeout()
public void commit()
public void rollback()
protected void commitOrRollback(boolean doCommit)
protected void commitAllResources() throws JtxException
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.
JtxException
protected void rollbackAllResources(boolean wasForced)
public <E> E requestResource(java.lang.Class<E> resourceType)
protected <E> E lookupResource(java.lang.Class<E> resourceType)
null
if resource not found.
Only open resources can be found.Copyright © 2003-present Jodd Team