public abstract class Transaction
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
static class |
Transaction.TransactionAwareExecution |
protected static interface |
Transaction.TransactionFactory |
protected class |
Transaction.TxInvalidationSet |
| Modifier and Type | Field and Description |
|---|---|
protected java.util.ArrayList<java.lang.Object> |
attachedObjects |
static boolean |
captureInvalidationStackTraces |
static java.lang.String |
CFG_ENABLE_TX_CACHE |
static java.lang.String |
CFG_ROLLBACK_ON_COMMIT_ERROR |
static java.lang.ThreadLocal<java.lang.Long> |
lastStartTL |
| Constructor and Description |
|---|
Transaction() |
| Modifier and Type | Method and Description |
|---|---|
void |
activateAsCurrentTransaction()
Makes this transaction the new current transaction.
|
void |
activateCache(boolean activate)
activated the cache for the current transaction (thread).
|
void |
addDelayedConstraint(Item item,
Item.ItemConstraint constr) |
void |
addToDelayedRemoval(EntityInstance entity) |
void |
addToDelayedRollbackLocalInvalidations(java.lang.Object[] key,
int type,
int topicdepth) |
protected void |
assertNoCurrentTransactionRunning() |
void |
attach(java.lang.Object objectToAttach) |
void |
begin() |
protected void |
beginOuter() |
protected ConnectionImpl |
bindConnection(Tenant tenant,
boolean userTAEnabled) |
protected boolean |
calculatedUserTAEnabled(Tenant t) |
protected void |
checkBeforeCommit() |
protected void |
checkBeforeRollback() |
protected void |
checkDelayedConstrains() |
protected void |
checkForOtherCurrentTxRunning() |
void |
clearDelayedConstrains(PK itemPK) |
protected void |
clearRollbackOnly() |
protected void |
clearTxBoundConnectionAndNotify(boolean isCommit) |
protected void |
clearTxBoundConnectionAndNotifyCommit() |
protected void |
clearTxBoundConnectionAndNotifyRollback() |
void |
commit() |
protected void |
commitConnectionAndClearAndUnsetAsCurrent() |
protected static Transaction |
createNew() |
static Transaction |
current()
Returns the current transaction instance.
|
protected void |
decreaseOpenTransactionCount() |
boolean |
dettach(java.lang.Object objectToAttach) |
void |
enableDelayedStore(boolean delay)
the "delayed store" feature enables you to delay all item modifications until the end of the transaction.
|
void |
enableTxCache(boolean enable)
For a running transaction this method allow to define whether or not a local cache should be used.
|
static void |
enableUserTransactionForThread(boolean enable)
Enable or disable the user of UserTransactions for the current thread.
|
java.lang.Object |
execute(TransactionBody transactionBody)
Executes given code inside the current transaction.
|
java.lang.Object |
execute(TransactionBody transactionBody,
java.lang.Class<? extends java.lang.Exception>... permittedExceptions)
Executes given code inside the current transaction.
|
boolean |
executeOnCommit(Transaction.TransactionAwareExecution ex) |
void |
executeOnRollback(Transaction.TransactionAwareExecution ex) |
void |
executeOrDelayStore(EntityInstance entity) |
protected void |
finishExecute(java.lang.Throwable thrown,
TransactionBody body,
java.lang.Class<? extends java.lang.Exception>... permittedExceptions) |
void |
flushDelayedStore()
All entity modification delayed within the current transaction will be made persistent right now.
|
protected void |
flushDelayedStore(EntityInstance entity) |
protected AfterSaveListenerRegistry |
getAfterSaveEventListenerRegistry() |
<T> T |
getAttached(java.lang.Class<T> classOfAttachedObject) |
EntityInstance |
getAttachedEntityInstance(PK pk) |
java.util.Queue<java.lang.Throwable> |
getBeginTransactionStack()
Returns a stack frame for currents transaction
begin(). |
protected ConnectionImpl |
getConnectionToBind(HybrisDataSource dataSource) |
java.lang.Object |
getContextEntry(java.lang.Object key) |
protected static Transaction |
getCurrentIfExists() |
protected AfterSaveEventChangesCollector |
getEntityChangesCollector() |
long |
getObjectID()
returns a unique id for this Transaction object.
|
int |
getOpenTransactionCount()
Returns the number of transactions that are open for this thread, which is of course 0 if no transaction is running,
1 if a transaction is running, 2 if we have a nested transaction and so on.
|
protected TransactionAwareCache |
getOrCreateGlobalCacheAdapter(Cache globalCache) |
protected TransactionAwareCache |
getOrCreateLegacyCacheAdapter(Cache globalCache) |
protected TransactionAwareCache |
getOrCreateTxLocalCacheAdapter(Cache globalCache) |
EntityInstance |
getOrLoadTxBoundEntityInstance(PersistencePool pool,
java.lang.String jndi,
PK pk) |
TransactionAwareCache |
getTransactionAwareCache(Cache globalCache) |
protected static Transaction.TransactionFactory |
getTransactionsFactory() |
ConnectionImpl |
getTXBoundConnection()
Inside a transaction this method returns the transaction bound database connection.
|
protected void |
increaseOpenTransactionCount() |
void |
invalidate(AbstractCacheUnit unit,
int invalidationType) |
void |
invalidate(AbstractCacheUnit unit,
int invalidationType,
boolean sendImmediately) |
void |
invalidate(java.lang.Object[] key,
int invalidationTopicDepth,
int invalidationType) |
void |
invalidate(java.lang.Object[] key,
int invalidationTopicDepth,
int invalidationType,
boolean sendImmediately) |
void |
invalidateAndNotifyCommit(java.lang.Object[] key,
int invalidationDepth,
int invalidationType) |
void |
invalidateAndNotifyRollback(java.lang.Object[] key,
int invalidationDepth,
int invalidationType) |
void |
invalidateFromDirectPersistence(java.lang.Object[] key,
PK pk,
int invalidationType) |
boolean |
isCurrent() |
boolean |
isDelayedStoreEnabled()
returns true if all entity modifications should be delayed until the end of the TX.
|
protected boolean |
isExceptionIsPermitted(java.lang.Throwable thrown,
java.lang.Class<? extends java.lang.Exception>... permittedExceptions) |
static boolean |
isInCommitOrRollback()
Checks whether there is a transaction running in the current thread and this transaction is in commit or rollback
phase (including invalidation phase).
|
boolean |
isInvalidated(java.lang.Object[] key) |
boolean |
isNested() |
boolean |
isRollbackOnCommitError() |
boolean |
isRollbackOnly()
Tells if this transaction has been marked as rollback-only.
|
boolean |
isRunning()
Tells if there is currently a active transaction.
|
boolean |
isTxCacheEnabled()
Tells whether this (running) transaction is using a local cache for all lookups which cannot be backed by the global
2n level cache due to this transaction having invalidation recorded affecting the lookup.
|
static boolean |
isUserTransactionEnabled()
check the value set using the enableUserTransactionForThread(Boolean) method.
|
protected void |
loadAfterSaveListenerRegistry() |
void |
lock(Item item)
Lock's an entity for update, performing a row-level lock on the database.
|
protected void |
logBeforeBegin() |
protected void |
logBeforeCommit() |
protected void |
logBeforeRollback() |
void |
notifyCommit() |
void |
notifyRollback() |
protected void |
onNestedBeginError(java.lang.Exception e) |
protected void |
onOuterBeginError(java.lang.Exception e) |
static Transaction |
performCommitBeginEvery(Transaction existingTransaction,
int ms)
Helper method to commit a transaction after it's running over given amount of time.
|
void |
printContextInfo() |
void |
registerEntityInstance(EntityInstance instance) |
void |
reloadEntityInstance(PK pk) |
protected void |
removeFromEntityMap(PK pk) |
void |
rollback() |
protected void |
rollbackConnection() |
protected void |
rollbackConnection(ConnectionImpl con) |
protected void |
rollbackOuter() |
protected void |
setAsCurrent() |
void |
setContextEntry(java.lang.Object key,
java.lang.Object value) |
void |
setRollbackOnCommitError(boolean rollback)
If set to
true any error happening during database commit will cause also a rollback on database level. |
void |
setRollbackOnly()
Marks this transaction as rollback only.
|
protected static void |
setTransactionFactory(Transaction.TransactionFactory transactionFactory) |
void |
setTransactionIsolationLevel(int level)
Allows to choose the transaction isolation level to be used for starting a new transaction.
|
static <ET extends java.lang.Exception> |
toException(java.lang.Throwable e,
java.lang.Class<ET> businessExceptionClass)
Utility method to help handling business exceptions coming from
execute(TransactionBody) method. |
protected static void |
unsetTransactionFactory() |
protected void |
unsetTxBoundConnection() |
boolean |
useCache(AbstractCacheUnit cache) |
protected abstract boolean |
useCacheInternal(AbstractCacheUnit cacheUnit)
by implementing this method subclasses can influence the result of
useCache(AbstractCacheUnit) |
public static java.lang.String CFG_ROLLBACK_ON_COMMIT_ERROR
public static final java.lang.String CFG_ENABLE_TX_CACHE
public static boolean captureInvalidationStackTraces
public static final java.lang.ThreadLocal<java.lang.Long> lastStartTL
protected final java.util.ArrayList<java.lang.Object> attachedObjects
protected static void setTransactionFactory(Transaction.TransactionFactory transactionFactory)
protected static void unsetTransactionFactory()
protected static Transaction.TransactionFactory getTransactionsFactory()
protected AfterSaveEventChangesCollector getEntityChangesCollector()
public void printContextInfo()
public java.util.Queue<java.lang.Throwable> getBeginTransactionStack()
begin(). For diagnostic purposes only.public static Transaction current()
begin()
does not create new transaction instances. After finishing the (outermost) transaction via commit()
or rollback() the instance is discarded. The next call to current() creates a new instance.protected static Transaction getCurrentIfExists()
public boolean isCurrent()
protected static Transaction createNew()
public long getObjectID()
public final boolean isRunning()
begin() has been called the transaction is considered
to be active. After calling commit() or rollback() the transaction is no longer active. If nested
transactions are started it returns true until commit() or rollback() has been called
on the outermost transaction even if inner transactions have been ended via commit or rollback.public boolean isNested()
public final void setRollbackOnly()
commit() afterwards will in fact roll back the transaction and throw a
TransactionException to indicate that commit has not been successful (only outermost transaction - nested
transactions do not throw a exception). Example:
Transaction.current().begin();
{
Transaction.current().begin();
{
Transaction.current().setRollbackOnly();
}
Transaction.current().commit(); // no exception here; no commit or rollback either
}
Transaction.current().commit(); // throws exception + does rollback
public boolean isRollbackOnly()
setRollbackOnly() or
rollback() upon a nested transaction. See setRollbackOnly() for more details.protected void clearRollbackOnly()
public static boolean isInCommitOrRollback()
public int getOpenTransactionCount()
protected void increaseOpenTransactionCount()
protected void decreaseOpenTransactionCount()
protected boolean calculatedUserTAEnabled(Tenant t)
public void begin()
throws TransactionException
TransactionExceptionprotected void onNestedBeginError(java.lang.Exception e)
protected void onOuterBeginError(java.lang.Exception e)
protected void checkForOtherCurrentTxRunning()
protected void beginOuter()
protected void loadAfterSaveListenerRegistry()
protected AfterSaveListenerRegistry getAfterSaveEventListenerRegistry()
protected ConnectionImpl bindConnection(Tenant tenant, boolean userTAEnabled)
protected void logBeforeBegin()
protected ConnectionImpl getConnectionToBind(HybrisDataSource dataSource) throws java.sql.SQLException
java.sql.SQLExceptionpublic void commit()
throws TransactionException
TransactionExceptionprotected void commitConnectionAndClearAndUnsetAsCurrent()
protected void checkBeforeCommit()
protected void logBeforeCommit()
public static <ET extends java.lang.Exception> ET toException(java.lang.Throwable e,
java.lang.Class<ET> businessExceptionClass)
execute(TransactionBody) method.protected void clearTxBoundConnectionAndNotifyCommit()
protected void clearTxBoundConnectionAndNotifyRollback()
protected void clearTxBoundConnectionAndNotify(boolean isCommit)
protected void unsetTxBoundConnection()
protected void checkDelayedConstrains()
throws ConsistencyCheckException
ConsistencyCheckExceptionpublic void invalidateAndNotifyCommit(java.lang.Object[] key,
int invalidationDepth,
int invalidationType)
public void notifyCommit()
public void rollback()
throws TransactionException
TransactionExceptionprotected void checkBeforeRollback()
protected void logBeforeRollback()
protected void rollbackOuter()
protected void rollbackConnection()
throws java.sql.SQLException
java.sql.SQLExceptionprotected void rollbackConnection(ConnectionImpl con) throws java.sql.SQLException
java.sql.SQLExceptionpublic void invalidateAndNotifyRollback(java.lang.Object[] key,
int invalidationDepth,
int invalidationType)
public void notifyRollback()
public java.lang.Object execute(TransactionBody transactionBody) throws java.lang.Exception
begin nor commit is called since
the enclosing transaction will do that.
In case an exception is raised and this method started the transaction itself it will rollback any changes. If no own transaction was started the exception is simply thrown to the calling code.
transactionBody - the code to execute within this transactionjava.lang.Exception - in case the executed code raised an exceptionpublic java.lang.Object execute(TransactionBody transactionBody, java.lang.Class<? extends java.lang.Exception>... permittedExceptions) throws java.lang.Exception
begin nor commit is called since
the enclosing transaction will do that.
In case an exception is raised and this method started the transaction itself it will rollback any changes. If no own transaction was started the exception is simply thrown to the calling code.
transactionBody - the code to execute within this transactionpermittedExceptions - a list of exception classes which are actually allowed to be thrown without the transaction being
rolled back; may be null if no exceptions are allowed to do sojava.lang.Exception - in case the executed code raised an exceptionprotected void finishExecute(java.lang.Throwable thrown,
TransactionBody body,
java.lang.Class<? extends java.lang.Exception>... permittedExceptions)
throws java.lang.Exception
java.lang.Exceptionprotected boolean isExceptionIsPermitted(java.lang.Throwable thrown,
java.lang.Class<? extends java.lang.Exception>... permittedExceptions)
public java.lang.Object getContextEntry(java.lang.Object key)
public void setContextEntry(java.lang.Object key,
java.lang.Object value)
public void invalidate(AbstractCacheUnit unit, int invalidationType)
public void invalidate(AbstractCacheUnit unit, int invalidationType, boolean sendImmediately)
public void invalidate(java.lang.Object[] key,
int invalidationTopicDepth,
int invalidationType)
public void invalidateFromDirectPersistence(java.lang.Object[] key,
PK pk,
int invalidationType)
protected void removeFromEntityMap(PK pk)
public void invalidate(java.lang.Object[] key,
int invalidationTopicDepth,
int invalidationType,
boolean sendImmediately)
public void addToDelayedRollbackLocalInvalidations(java.lang.Object[] key,
int type,
int topicdepth)
public final boolean useCache(AbstractCacheUnit cache)
public void activateCache(boolean activate)
activate - true to activate, false to deactivateprotected abstract boolean useCacheInternal(AbstractCacheUnit cacheUnit)
useCache(AbstractCacheUnit)public boolean isInvalidated(java.lang.Object[] key)
public void enableDelayedStore(boolean delay)
public void setRollbackOnCommitError(boolean rollback)
true any error happening during database commit will cause also a rollback on database level.
Normally this is not necessary but should be considered in case database transactions are still open after commit.
Also see configuration parameter 'transaction.rollbackOnCommitError' which allows to enable this globally.public boolean isRollbackOnCommitError()
public boolean isDelayedStoreEnabled()
public void flushDelayedStore()
rollback-only!protected void flushDelayedStore(EntityInstance entity)
public void executeOrDelayStore(EntityInstance entity)
public void addToDelayedRemoval(EntityInstance entity)
public void executeOnRollback(Transaction.TransactionAwareExecution ex)
public boolean executeOnCommit(Transaction.TransactionAwareExecution ex)
public static void enableUserTransactionForThread(boolean enable)
enable - true to enable, false to disable UserTransactions for the current threadpublic static boolean isUserTransactionEnabled()
public void activateAsCurrentTransaction()
java.lang.IllegalStateException - if there is still a current transaction set which is running.protected void assertNoCurrentTransactionRunning()
protected void setAsCurrent()
public void registerEntityInstance(EntityInstance instance)
public EntityInstance getOrLoadTxBoundEntityInstance(PersistencePool pool, java.lang.String jndi, PK pk)
pool - jndi - pk - YNoSuchEntityException - is there is no item for the given pkpublic EntityInstance getAttachedEntityInstance(PK pk)
public void reloadEntityInstance(PK pk)
public ConnectionImpl getTXBoundConnection()
java.lang.IllegalStateException - if called outside a transactionpublic void setTransactionIsolationLevel(int level)
Connection.setTransactionIsolation(int) for details)!
Use as follows:
Transaction tx = Transaction.current(); tx.setTransactionIsolationLevel(Connection.TRANSACTION_READ_COMMITTED); tx.begin(); // ... business code tx.commit();Also note that after commit or rolling back the isolation level setting is cleared - it has to be set for each transaction anew!
level - the level - see Connection for detailspublic static Transaction performCommitBeginEvery(Transaction existingTransaction, int ms)
existingTransaction - existing transactionms - transaction time thresholdpublic void clearDelayedConstrains(PK itemPK)
public void addDelayedConstraint(Item item, Item.ItemConstraint constr)
public void lock(Item item) throws java.lang.IllegalStateException
Calls to lock can only be made between calls to begin() and commit() or
rollback(). All locks that are acquired during a transaction are automatically released on calls to
commit() or rollback(). If a lock is held by another transaction, this method will block until the
lock can be acquired.
Depending on a the DBMS, a lock on a single row may result in locking an entire page. Locks should not be acquired on long-running transactions unless absolutely necessary. Note, that on some DBMS's, such as HSQLDB, row-level locking is not supported and any attempt to perform a lock will throw an UnsupportedOperationException.
Also note that it's crucial to use the correct isolation level for getting
the correct result after locking a item. For instance if you expect that a attribute ma have been changed while
waiting for that lock it may be a good idea to use Connection.TRANSACTION_READ_COMMITTED to allow reading
these changed values!
item - The entity to lockjava.lang.NullPointerException - If an null value is passed into the lock method or if no primary key has been assigned to the
entity.java.lang.IllegalStateException - If an attempt is made to lock an entity outside the bounds of the transaction, or an attempt is made to
lock an entity that does not exist in the database.java.lang.UnsupportedOperationException - If the database does not support SELECT FOR UPDATEpublic TransactionAwareCache getTransactionAwareCache(Cache globalCache)
protected TransactionAwareCache getOrCreateGlobalCacheAdapter(Cache globalCache)
protected TransactionAwareCache getOrCreateTxLocalCacheAdapter(Cache globalCache)
protected TransactionAwareCache getOrCreateLegacyCacheAdapter(Cache globalCache)
public boolean isTxCacheEnabled()
true and has the config setting CFG_ENABLE_TX_CACHE.enableTxCache(boolean)public void enableTxCache(boolean enable)
isTxCacheEnabled()public void attach(java.lang.Object objectToAttach)
public boolean dettach(java.lang.Object objectToAttach)
public <T> T getAttached(java.lang.Class<T> classOfAttachedObject)
Copyright © 2018 SAP SE. All Rights Reserved.