public abstract class Transaction extends 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 |
|---|---|
static boolean |
captureInvalidationStackTraces |
static String |
CFG_ENABLE_TX_CACHE |
static String |
CFG_ROLLBACK_ON_COMMIT_ERROR |
static ThreadLocal<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(Object[] key,
int type,
int topicdepth) |
protected void |
assertNoCurrentTransactionRunning() |
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() |
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.
|
Object |
execute(TransactionBody transactionBody)
Executes given code inside the current transaction.
|
Object |
execute(TransactionBody transactionBody,
Class<? extends 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(Throwable thrown,
TransactionBody body,
Class<? extends 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() |
EntityInstance |
getAttachedEntityInstance(PK pk) |
Queue<Throwable> |
getBeginTransactionStack()
Returns a stack frame for currents transaction
begin(). |
protected ConnectionImpl |
getConnectionToBind(HybrisDataSource dataSource) |
Object |
getContextEntry(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,
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(Object[] key,
int invalidationTopicDepth,
int invalidationType) |
void |
invalidate(Object[] key,
int invalidationTopicDepth,
int invalidationType,
boolean sendImmediately) |
void |
invalidateAndNotifyCommit(Object[] key,
int invalidationDepth,
int invalidationType) |
void |
invalidateAndNotifyRollback(Object[] key,
int invalidationDepth,
int invalidationType) |
void |
invalidateFromDirectPersistence(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(Throwable thrown,
Class<? extends 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(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.
|
protected boolean |
isUserTAEnabled()
Deprecated.
|
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(Exception e) |
protected void |
onOuterBeginError(Exception e) |
static void |
performCommitBeginEvery(int ms)
Deprecated.
|
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(Object key,
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 Exception> |
toException(Throwable e,
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 String CFG_ROLLBACK_ON_COMMIT_ERROR
public static final String CFG_ENABLE_TX_CACHE
public static boolean captureInvalidationStackTraces
public static final ThreadLocal<Long> lastStartTL
protected static void setTransactionFactory(Transaction.TransactionFactory transactionFactory)
protected static void unsetTransactionFactory()
protected static Transaction.TransactionFactory getTransactionsFactory()
protected AfterSaveEventChangesCollector getEntityChangesCollector()
public void printContextInfo()
public Queue<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()
@Deprecated protected boolean isUserTAEnabled()
protected boolean calculatedUserTAEnabled(Tenant t)
public void begin()
throws TransactionException
TransactionExceptionprotected void onNestedBeginError(Exception e)
protected void onOuterBeginError(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 SQLException
SQLExceptionpublic void commit()
throws TransactionException
TransactionExceptionprotected void commitConnectionAndClearAndUnsetAsCurrent()
protected void checkBeforeCommit()
protected void logBeforeCommit()
public static <ET extends Exception> ET toException(Throwable e, 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(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 SQLException
SQLExceptionprotected void rollbackConnection(ConnectionImpl con) throws SQLException
SQLExceptionpublic void invalidateAndNotifyRollback(Object[] key, int invalidationDepth, int invalidationType)
public void notifyRollback()
public Object execute(TransactionBody transactionBody) throws 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 transactionException - in case the executed code raised an exceptionpublic Object execute(TransactionBody transactionBody, Class<? extends Exception>... permittedExceptions) throws 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 soException - in case the executed code raised an exceptionprotected void finishExecute(Throwable thrown, TransactionBody body, Class<? extends Exception>... permittedExceptions) throws Exception
Exceptionprotected boolean isExceptionIsPermitted(Throwable thrown, Class<? extends Exception>... permittedExceptions)
public void invalidate(AbstractCacheUnit unit, int invalidationType)
public void invalidate(AbstractCacheUnit unit, int invalidationType, boolean sendImmediately)
public void invalidate(Object[] key, int invalidationTopicDepth, int invalidationType)
public void invalidateFromDirectPersistence(Object[] key, PK pk, int invalidationType)
protected void removeFromEntityMap(PK pk)
public void invalidate(Object[] key, int invalidationTopicDepth, int invalidationType, boolean sendImmediately)
public void addToDelayedRollbackLocalInvalidations(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(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()
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, 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()
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 details@Deprecated public static void performCommitBeginEvery(int ms)
public 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 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 lockNullPointerException - If an null value is passed into the lock method or if no primary key has been assigned to
the entity.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.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()Copyright © 2017 SAP SE. All Rights Reserved.