Package org.h2.mvstore.tx
Class Transaction
java.lang.Object
org.h2.mvstore.tx.Transaction
A transaction.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Object
Key in blockingMap on which this transaction is blocked.private String
Map on which this transaction is blocked.private Transaction
Blocking transaction, if any(package private) final IsolationLevel
The current isolation level.(package private) final TransactionStore.RollbackListener
Listener for this transaction's rollback changes.(package private) static final int
How 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 final int
private static final long
private static final long
private String
Transaction name.private boolean
Whether other transaction(s) are waiting for this to close.private final int
Identification of the owner of this transaction, usually the owner is a database session.(package private) final long
This is really a transaction identity, because it's not re-used.private static final int
static final int
The status of a closed transaction (committed or rolled back).static final int
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.private static final int
private static final String[]
static final int
The status of an open transaction.static final int
The status of a prepared transaction.private static final int
The status of a transaction that has been rolled back completely, but undo operations are not finished yet.private static final int
The status of a transaction that currently in a process of rolling back to a savepoint.private final AtomicLong
(package private) final TransactionStore
The transaction store.(package private) int
How long to wait for blocking transaction to commit or rollback.(package private) final int
The transaction id.private final Map
<Integer, TransactionMap<?, ?>> Map of transactional maps for this transactionprivate MVStore.TxCounter
Reference to a counter for an earliest store version used by this transaction.private RootReference<Long,
Record<?, ?>>[] RootReferences for undo log snapshots(package private) boolean
Indicates whether this transaction was stored in preparedTransactions map -
Constructor Summary
ConstructorsConstructorDescriptionTransaction
(TransactionStore store, int transactionId, long sequenceNum, int status, String name, long logId, int timeoutMillis, int ownerId, IsolationLevel isolationLevel, TransactionStore.RollbackListener listener) -
Method Summary
Modifier and TypeMethodDescriptionboolean
Whether this transaction has isolation level READ_COMMITTED or below.private void
Check whether this transaction is open or prepared.private void
checkOpen
(int status) Check whether this transaction is open.(package private) void
closeIt()
Transition this transaction into a closed state.void
commit()
Commit the transaction.private static long
composeState
(int status, long logId, boolean hasRollback) int
getChanges
(long savepointId) Get the list of changes, starting with the latest change, up to the given savepoint (in reverse order than they occurred).int
getId()
Returns the isolation level of this transaction.private long
getLogId()
private static long
getLogId
(long state) getName()
long
int
private static int
getStatus
(long state) private static String
getStatusName
(int status) (package private) RootReference<Long,
Record<?, ?>>[] boolean
Determine if any database changes were made as part of this transaction.private static boolean
hasChanges
(long state) private static boolean
hasRollback
(long state) boolean
Returns whether statement dependencies are currently set.private static boolean
isActive
(int status) private boolean
isDeadlocked
(Transaction toWaitFor) (package private) boolean
(package private) long
Add a log entry.(package private) void
logUndo()
Remove the last log entry.void
Mark an exit from SQL statement execution within this transaction.void
markStatementStart
(HashSet<MVMap<Object, VersionedValue<Object>>> maps) Mark an entry into a new SQL statement execution within this transaction.private void
private void
<K,
V> TransactionMap <K, V> Open a data map.<K,
V> TransactionMap <K, V> 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.void
prepare()
Prepare the transaction.private void
<K,
V> void removeMap
(TransactionMap<K, V> map) Remove the map.void
rollback()
Roll the transaction back.void
rollbackToSavepoint
(long savepointId) Roll back to the given savepoint.void
long
Create a new savepoint.private long
setStatus
(int status) Changes transaction status to a specified valuevoid
setTimeoutMillis
(int timeoutMillis) Sets the new lock timeout.private String
private static String
stateToString
(long state) toString()
private void
tryThrowDeadLockException
(boolean throwIt) boolean
waitFor
(Transaction toWaitFor, String mapName, 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 boolean
waitForThisToEnd
(int millis, Transaction waiter)
-
Field Details
-
STATUS_CLOSED
public static final int STATUS_CLOSEDThe status of a closed transaction (committed or rolled back).- See Also:
-
STATUS_OPEN
public static final int STATUS_OPENThe status of an open transaction.- See Also:
-
STATUS_PREPARED
public static final int STATUS_PREPAREDThe status of a prepared transaction.- See Also:
-
STATUS_COMMITTED
public static final int STATUS_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. 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:
-
STATUS_ROLLING_BACK
private static final int STATUS_ROLLING_BACKThe status of a transaction that currently in a process of rolling back to a savepoint.- See Also:
-
STATUS_ROLLED_BACK
private static final int STATUS_ROLLED_BACKThe status of a transaction that has been rolled back completely, but undo operations are not finished yet.- See Also:
-
STATUS_NAMES
-
LOG_ID_BITS
static final int LOG_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).- See Also:
-
LOG_ID_BITS1
private static final int LOG_ID_BITS1- See Also:
-
LOG_ID_LIMIT
private static final long LOG_ID_LIMIT- See Also:
-
LOG_ID_MASK
private static final long LOG_ID_MASK- See Also:
-
STATUS_BITS
private static final int STATUS_BITS- See Also:
-
STATUS_MASK
private static final int STATUS_MASK- See Also:
-
store
The transaction store. -
listener
Listener for this transaction's rollback changes. -
transactionId
final int transactionIdThe transaction id. More appropriate name for this field would be "slotId" -
sequenceNum
final long sequenceNumThis is really a transaction identity, because it's not re-used. -
statusAndLogId
-
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
Transaction name. -
wasStored
boolean wasStoredIndicates whether this transaction was stored in preparedTransactions map -
timeoutMillis
int timeoutMillisHow long to wait for blocking transaction to commit or rollback. -
ownerId
private final int ownerIdIdentification of the owner of this transaction, usually the owner is a database session. -
blockingTransaction
Blocking transaction, if any -
blockingMapName
Map on which this transaction is blocked. -
blockingKey
Key in blockingMap on which this transaction is blocked. -
notificationRequested
private volatile boolean notificationRequestedWhether other transaction(s) are waiting for this to close. -
undoLogRootReferences
RootReferences for undo log snapshots -
transactionMaps
Map of transactional maps for this transaction -
isolationLevel
The current isolation level.
-
-
Constructor Details
-
Transaction
Transaction(TransactionStore store, int transactionId, long sequenceNum, int status, String name, long logId, int timeoutMillis, int ownerId, IsolationLevel isolationLevel, TransactionStore.RollbackListener listener)
-
-
Method Details
-
getId
public int getId() -
getSequenceNum
public long getSequenceNum() -
getStatus
public int getStatus() -
getUndoLogRootReferences
RootReference<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
-
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
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
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
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
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
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
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
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
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
-
tryThrowDeadLockException
private void tryThrowDeadLockException(boolean throwIt) -
waitForThisToEnd
-
removeMap
Remove the map.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
map
- the map
-
toString
-
stateToString
-
stateToString
-
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
-