Package org.h2.mvstore.tx
Class Transaction
- java.lang.Object
-
- org.h2.mvstore.tx.Transaction
-
public final class Transaction extends java.lang.ObjectA transaction.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.ObjectblockingKeyKey in blockingMap on which this transaction is blocked.private java.lang.StringblockingMapNameMap on which this transaction is blocked.private TransactionblockingTransactionBlocking transaction, if any(package private) IsolationLevelisolationLevelThe current isolation level.(package private) TransactionStore.RollbackListenerlistenerListener for this transaction's rollback changes.(package private) static intLOG_ID_BITSHow many bits of the "operation id" we store in the transaction belong to the log id (the rest belong to the transaction id).private static intLOG_ID_BITS1private static longLOG_ID_LIMITprivate static longLOG_ID_MASKprivate java.lang.StringnameTransaction name.private booleannotificationRequestedWhether other transaction(s) are waiting for this to close.private intownerIdIdentification of the owner of this transaction, usually the owner is a database session.(package private) longsequenceNumThis is really a transaction identity, because it's not re-used.private static intSTATUS_BITSstatic intSTATUS_CLOSEDThe status of a closed transaction (committed or rolled back).static intSTATUS_COMMITTEDThe status of a transaction that has been logically committed or rather marked as committed, because it might be still listed among prepared, if it was prepared for commit.private static intSTATUS_MASKprivate static java.lang.String[]STATUS_NAMESstatic intSTATUS_OPENThe status of an open transaction.static intSTATUS_PREPAREDThe status of a prepared transaction.private static intSTATUS_ROLLED_BACKThe status of a transaction that has been rolled back completely, but undo operations are not finished yet.private static intSTATUS_ROLLING_BACKThe status of a transaction that currently in a process of rolling back to a savepoint.private java.util.concurrent.atomic.AtomicLongstatusAndLogId(package private) TransactionStorestoreThe transaction store.(package private) inttimeoutMillisHow long to wait for blocking transaction to commit or rollback.(package private) inttransactionIdThe transaction id.private java.util.Map<java.lang.Integer,TransactionMap<?,?>>transactionMapsMap of transactional maps for this transactionprivate MVStore.TxCountertxCounterReference to a counter for an earliest store version used by this transaction.private RootReference<java.lang.Long,Record<?,?>>[]undoLogRootReferencesRootReferences for undo log snapshots(package private) booleanwasStoredIndicates whether this transaction was stored in preparedTransactions map
-
Constructor Summary
Constructors Constructor Description Transaction(TransactionStore store, int transactionId, long sequenceNum, int status, java.lang.String name, long logId, int timeoutMillis, int ownerId, IsolationLevel isolationLevel, TransactionStore.RollbackListener listener)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanallowNonRepeatableRead()Whether this transaction has isolation level READ_COMMITTED or below.private voidcheckNotClosed()Check whether this transaction is open or prepared.private voidcheckOpen(int status)Check whether this transaction is open.(package private) voidcloseIt()Transition this transaction into a closed state.voidcommit()Commit the transaction.private static longcomposeState(int status, long logId, boolean hasRollback)intgetBlockerId()java.util.Iterator<TransactionStore.Change>getChanges(long savepointId)Get the list of changes, starting with the latest change, up to the given savepoint (in reverse order than they occurred).intgetId()IsolationLevelgetIsolationLevel()Returns the isolation level of this transaction.private longgetLogId()private static longgetLogId(long state)java.lang.StringgetName()longgetSequenceNum()intgetStatus()private static intgetStatus(long state)private static java.lang.StringgetStatusName(int status)(package private) RootReference<java.lang.Long,Record<?,?>>[]getUndoLogRootReferences()booleanhasChanges()Determine if any database changes were made as part of this transaction.private static booleanhasChanges(long state)private static booleanhasRollback(long state)booleanhasStatementDependencies()Returns whether statement dependencies are currently set.private static booleanisActive(int status)private booleanisDeadlocked(Transaction toWaitFor)(package private) booleanisReadCommitted()(package private) longlog(Record<?,?> logRecord)Add a log entry.(package private) voidlogUndo()Remove the last log entry.voidmarkStatementEnd()Mark an exit from SQL statement execution within this transaction.voidmarkStatementStart(java.util.HashSet<MVMap<java.lang.Object,VersionedValue<java.lang.Object>>> maps)Mark an entry into a new SQL statement execution within this transaction.private voidmarkTransactionEnd()private voidnotifyAllWaitingTransactions()<K,V>
TransactionMap<K,V>openMap(java.lang.String name)Open a data map.<K,V>
TransactionMap<K,V>openMap(java.lang.String name, DataType<K> keyType, DataType<V> valueType)Open the map to store the data.<K,V>
TransactionMap<K,V>openMapX(MVMap<K,VersionedValue<V>> map)Open the transactional version of the given map.voidprepare()Prepare the transaction.private voidreleaseSnapshot()<K,V>
voidremoveMap(TransactionMap<K,V> map)Remove the map.voidrollback()Roll the transaction back.voidrollbackToSavepoint(long savepointId)Roll back to the given savepoint.voidsetName(java.lang.String name)longsetSavepoint()Create a new savepoint.private longsetStatus(int status)Changes transaction status to a specified valuevoidsetTimeoutMillis(int timeoutMillis)Sets the new lock timeout.private java.lang.StringstateToString()private static java.lang.StringstateToString(long state)java.lang.StringtoString()private voidtryThrowDeadLockException(boolean throwIt)booleanwaitFor(Transaction toWaitFor, java.lang.String mapName, java.lang.Object key)Make this transaction to wait for the specified transaction to be closed, because both of them try to modify the same map entry.private booleanwaitForThisToEnd(int millis, Transaction waiter)
-
-
-
Field Detail
-
STATUS_CLOSED
public static final int STATUS_CLOSED
The status of a closed transaction (committed or rolled back).- See Also:
- Constant Field Values
-
STATUS_OPEN
public static final int STATUS_OPEN
The status of an open transaction.- See Also:
- Constant Field Values
-
STATUS_PREPARED
public static final int STATUS_PREPARED
The status of a prepared transaction.- See Also:
- Constant Field Values
-
STATUS_COMMITTED
public static final int STATUS_COMMITTED
The status of a transaction that has been logically committed or rather marked as committed, because it might be still listed among prepared, if it was prepared for commit. Undo log entries might still exists for it and not all of it's changes within map's are re-written as committed yet. Nevertheless, those changes should be already viewed by other transactions as committed. This transaction's id can not be re-used until all of the above is completed and transaction is closed. A transactions can be observed in this state when the store was closed while the transaction was not closed yet. When opening a store, such transactions will automatically be processed and closed as committed.- See Also:
- Constant Field Values
-
STATUS_ROLLING_BACK
private static final int STATUS_ROLLING_BACK
The status of a transaction that currently in a process of rolling back to a savepoint.- See Also:
- Constant Field Values
-
STATUS_ROLLED_BACK
private static final int STATUS_ROLLED_BACK
The status of a transaction that has been rolled back completely, but undo operations are not finished yet.- See Also:
- Constant Field Values
-
STATUS_NAMES
private static final java.lang.String[] STATUS_NAMES
-
LOG_ID_BITS
static final int LOG_ID_BITS
How many bits of the "operation id" we store in the transaction belong to the log id (the rest belong to the transaction id).- See Also:
- Constant Field Values
-
LOG_ID_BITS1
private static final int LOG_ID_BITS1
- See Also:
- Constant Field Values
-
LOG_ID_LIMIT
private static final long LOG_ID_LIMIT
- See Also:
- Constant Field Values
-
LOG_ID_MASK
private static final long LOG_ID_MASK
- See Also:
- Constant Field Values
-
STATUS_BITS
private static final int STATUS_BITS
- See Also:
- Constant Field Values
-
STATUS_MASK
private static final int STATUS_MASK
- See Also:
- Constant Field Values
-
store
final TransactionStore store
The transaction store.
-
listener
final TransactionStore.RollbackListener listener
Listener for this transaction's rollback changes.
-
transactionId
final int transactionId
The transaction id. More appropriate name for this field would be "slotId"
-
sequenceNum
final long sequenceNum
This is really a transaction identity, because it's not re-used.
-
statusAndLogId
private final java.util.concurrent.atomic.AtomicLong statusAndLogId
-
txCounter
private MVStore.TxCounter txCounter
Reference to a counter for an earliest store version used by this transaction. Referenced version and all newer ones can not be discarded at least until this transaction ends.
-
name
private java.lang.String name
Transaction name.
-
wasStored
boolean wasStored
Indicates whether this transaction was stored in preparedTransactions map
-
timeoutMillis
int timeoutMillis
How long to wait for blocking transaction to commit or rollback.
-
ownerId
private final int ownerId
Identification of the owner of this transaction, usually the owner is a database session.
-
blockingTransaction
private volatile Transaction blockingTransaction
Blocking transaction, if any
-
blockingMapName
private java.lang.String blockingMapName
Map on which this transaction is blocked.
-
blockingKey
private java.lang.Object blockingKey
Key in blockingMap on which this transaction is blocked.
-
notificationRequested
private volatile boolean notificationRequested
Whether other transaction(s) are waiting for this to close.
-
undoLogRootReferences
private RootReference<java.lang.Long,Record<?,?>>[] undoLogRootReferences
RootReferences for undo log snapshots
-
transactionMaps
private final java.util.Map<java.lang.Integer,TransactionMap<?,?>> transactionMaps
Map of transactional maps for this transaction
-
isolationLevel
final IsolationLevel isolationLevel
The current isolation level.
-
-
Constructor Detail
-
Transaction
Transaction(TransactionStore store, int transactionId, long sequenceNum, int status, java.lang.String name, long logId, int timeoutMillis, int ownerId, IsolationLevel isolationLevel, TransactionStore.RollbackListener listener)
-
-
Method Detail
-
getId
public int getId()
-
getSequenceNum
public long getSequenceNum()
-
getStatus
public int getStatus()
-
getUndoLogRootReferences
RootReference<java.lang.Long,Record<?,?>>[] getUndoLogRootReferences()
-
setStatus
private long setStatus(int status)
Changes transaction status to a specified value- Parameters:
status- to be set- Returns:
- transaction state as it was before status change
-
hasChanges
public boolean hasChanges()
Determine if any database changes were made as part of this transaction.- Returns:
- true if there are changes to commit, false otherwise
-
setName
public void setName(java.lang.String name)
-
getName
public java.lang.String getName()
-
getBlockerId
public int getBlockerId()
-
setSavepoint
public long setSavepoint()
Create a new savepoint.- Returns:
- the savepoint id
-
hasStatementDependencies
public boolean hasStatementDependencies()
Returns whether statement dependencies are currently set.- Returns:
- whether statement dependencies are currently set
-
getIsolationLevel
public IsolationLevel getIsolationLevel()
Returns the isolation level of this transaction.- Returns:
- the isolation level of this transaction
-
isReadCommitted
boolean isReadCommitted()
-
allowNonRepeatableRead
public boolean allowNonRepeatableRead()
Whether this transaction has isolation level READ_COMMITTED or below.- Returns:
- true if isolation level is READ_COMMITTED or READ_UNCOMMITTED
-
markStatementStart
public void markStatementStart(java.util.HashSet<MVMap<java.lang.Object,VersionedValue<java.lang.Object>>> maps)
Mark an entry into a new SQL statement execution within this transaction.- Parameters:
maps- set of maps used by transaction or statement is about to be executed
-
markStatementEnd
public void markStatementEnd()
Mark an exit from SQL statement execution within this transaction.
-
markTransactionEnd
private void markTransactionEnd()
-
releaseSnapshot
private void releaseSnapshot()
-
log
long log(Record<?,?> logRecord)
Add a log entry.- Parameters:
logRecord- to append- Returns:
- key for the newly added undo log entry
-
logUndo
void logUndo()
Remove the last log entry.
-
openMap
public <K,V> TransactionMap<K,V> openMap(java.lang.String name)
Open a data map.- Type Parameters:
K- the key typeV- the value type- Parameters:
name- the name of the map- Returns:
- the transaction map
-
openMap
public <K,V> TransactionMap<K,V> openMap(java.lang.String name, DataType<K> keyType, DataType<V> valueType)
Open the map to store the data.- Type Parameters:
K- the key typeV- the value type- Parameters:
name- the name of the mapkeyType- the key data typevalueType- the value data type- Returns:
- the transaction map
-
openMapX
public <K,V> TransactionMap<K,V> openMapX(MVMap<K,VersionedValue<V>> map)
Open the transactional version of the given map.- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the base map- Returns:
- the transactional map
-
prepare
public void prepare()
Prepare the transaction. Afterwards, the transaction can only be committed or completely rolled back.
-
commit
public void commit()
Commit the transaction. Afterwards, this transaction is closed.
-
rollbackToSavepoint
public void rollbackToSavepoint(long savepointId)
Roll back to the given savepoint. This is only allowed if the transaction is open.- Parameters:
savepointId- the savepoint id
-
rollback
public void rollback()
Roll the transaction back. Afterwards, this transaction is closed.
-
isActive
private static boolean isActive(int status)
-
getChanges
public java.util.Iterator<TransactionStore.Change> getChanges(long savepointId)
Get the list of changes, starting with the latest change, up to the given savepoint (in reverse order than they occurred). The value of the change is the value before the change was applied.- Parameters:
savepointId- the savepoint id, 0 meaning the beginning of the transaction- Returns:
- the changes
-
setTimeoutMillis
public void setTimeoutMillis(int timeoutMillis)
Sets the new lock timeout.- Parameters:
timeoutMillis- the new lock timeout in milliseconds
-
getLogId
private long getLogId()
-
checkOpen
private void checkOpen(int status)
Check whether this transaction is open.
-
checkNotClosed
private void checkNotClosed()
Check whether this transaction is open or prepared.
-
closeIt
void closeIt()
Transition this transaction into a closed state.
-
notifyAllWaitingTransactions
private void notifyAllWaitingTransactions()
-
waitFor
public boolean waitFor(Transaction toWaitFor, java.lang.String mapName, java.lang.Object key)
Make this transaction to wait for the specified transaction to be closed, because both of them try to modify the same map entry.- Parameters:
toWaitFor- transaction to wait formapName- name of the map containing blocking entrykey- of the blocking entry- Returns:
- true if other transaction was closed and this one can proceed, false if timed out
-
isDeadlocked
private boolean isDeadlocked(Transaction toWaitFor)
-
tryThrowDeadLockException
private void tryThrowDeadLockException(boolean throwIt)
-
waitForThisToEnd
private boolean waitForThisToEnd(int millis, Transaction waiter)
-
removeMap
public <K,V> void removeMap(TransactionMap<K,V> map)
Remove the map.- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the map
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
stateToString
private java.lang.String stateToString()
-
stateToString
private static java.lang.String stateToString(long state)
-
getStatus
private static int getStatus(long state)
-
getLogId
private static long getLogId(long state)
-
hasRollback
private static boolean hasRollback(long state)
-
hasChanges
private static boolean hasChanges(long state)
-
composeState
private static long composeState(int status, long logId, boolean hasRollback)
-
getStatusName
private static java.lang.String getStatusName(int status)
-
-