Package org.apache.ignite.transactions
Interface Transaction
-
- All Superinterfaces:
AutoCloseable,IgniteAsyncSupport
public interface Transaction extends AutoCloseable, IgniteAsyncSupport
Ignite cache transaction. Cache transactions have a default 2PC (two-phase-commit) behavior and can be plugged into ongoingJTAtransaction by properly implementingCacheTmLookupinterface. Cache transactions can also be started explicitly directly fromIgniteTransactionsAPI via any of the'IgniteTransactions.txStart(..)'methods.Cache transactions support the following isolation levels:
-
TransactionIsolation.READ_COMMITTEDisolation level means that always a committed value will be provided for read operations. With this isolation level values are always read from cache global memory or persistent store every time a value is accessed. In other words, if the same key is accessed more than once within the same transaction, it may have different value every time since global cache memory may be updated concurrently by other threads. -
TransactionIsolation.REPEATABLE_READisolation level means that if a value was read once within transaction, then all consecutive reads will provide the same in-transaction value. With this isolation level accessed values are stored within in-transaction memory, so consecutive access to the same key within the same transaction will always return the value that was previously read or updated within this transaction. If concurrency isTransactionConcurrency.PESSIMISTIC, then a lock on the key will be acquired prior to accessing the value. -
TransactionIsolation.SERIALIZABLEisolation level means that all transactions occur in a completely isolated fashion, as if all transactions in the system had executed serially, one after the other. Read access with this level happens the same way as withTransactionIsolation.REPEATABLE_READlevel. However, inTransactionConcurrency.OPTIMISTICmode, if some transactions cannot be serially isolated from each other, then one winner will be picked and the other transactions in conflict will result inTransactionOptimisticExceptionbeing thrown.
Cache transactions support the following concurrency models:
-
TransactionConcurrency.OPTIMISTIC- in this mode all cache operations are not distributed to other nodes untilcommit()is called. In this mode one'PREPARE'message will be sent to participating cache nodes to start acquiring per-transaction locks, and once all nodes reply'OK'(i.e.Phase 1completes successfully), a one-way''COMMIT'message is sent without waiting for reply. If it is necessary to know whenever remote nodes have committed as well, synchronous commit or synchronous rollback should be enabled viaCacheConfiguration.setWriteSynchronizationMode(org.apache.ignite.cache.CacheWriteSynchronizationMode).Note that in this mode, optimistic failures are only possible in conjunction with
TransactionIsolation.SERIALIZABLEisolation level. In all other cases, optimistic transactions will never fail optimistically and will always be identically ordered on all participating grid nodes. -
TransactionConcurrency.PESSIMISTIC- in this mode a lock is acquired on all cache operations with exception of read operations inTransactionIsolation.READ_COMMITTEDmode. All optional filters passed into cache operations will be evaluated after successful lock acquisition. Whenevercommit()is called, a single one-way'COMMIT'message is sent to participating cache nodes without waiting for reply. Note that there is no reason for distributed 'PREPARE' step, as all locks have been already acquired. Just like with optimistic mode, it is possible to configure synchronous commit or rollback and wait till transaction commits on all participating remote nodes.
Cache Atomicity Mode
In addition to standardCacheAtomicityMode.TRANSACTIONALbehavior, Ignite also supports a lighterCacheAtomicityMode.ATOMICmode as well. In this mode distributed transactions and distributed locking are not supported. Disabling transactions and locking allows to achieve much higher performance and throughput ratios. It is recommended thatCacheAtomicityMode.ATOMICmode is used whenever fullACID-compliant transactions are not needed.Usage
You can use cache transactions as follows:Ignite ignite = Ignition.ignite(); IgniteCache<String, Integer> cache = ignite.cache(cacheName); try (Transaction tx = ignite.transactions().txStart()) { // Perform transactional operations. Integer v1 = cache.get("k1"); // Check if v1 satisfies some condition before doing a put. if (v1 != null && v1 > 0) cache.put("k1", 2); cache.remove("k2"); // Commit the transaction. tx.commit(); }
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidclose()Ends the transaction.voidcommit()Commits this transaction by initiatingtwo-phase-commitprocess.IgniteFuture<Void>commitAsync()Asynchronously commits this transaction by initiatingtwo-phase-commitprocess.TransactionConcurrencyconcurrency()Cache transaction concurrency mode.booleanimplicit()Flag indicating whether transaction was started automatically by the system or not.booleanisInvalidate()Get invalidation flag for this transaction.TransactionIsolationisolation()Cache transaction isolation level.booleanisRollbackOnly()If transaction was marked as rollback-only.@Nullable Stringlabel()Returns transaction's label.UUIDnodeId()ID of the node on which this transaction started.voidresume()Resume a transaction if it was previously suspended.voidrollback()Rolls back this transaction.IgniteFuture<Void>rollbackAsync()Asynchronously rolls back this transaction.booleansetRollbackOnly()Modify the transaction associated with the current thread such that the only possible outcome of the transaction is to roll back the transaction.longstartTime()Start time of this transaction.TransactionStatestate()Gets current transaction state value.voidsuspend()Suspends a transaction.longthreadId()ID of the thread in which this transaction started.longtimeout()Gets timeout value in milliseconds for this transaction.longtimeout(long timeout)Sets transaction timeout value.IgniteUuidxid()Gets unique identifier for this transaction.-
Methods inherited from interface org.apache.ignite.lang.IgniteAsyncSupport
future, isAsync, withAsync
-
-
-
-
Method Detail
-
xid
IgniteUuid xid()
Gets unique identifier for this transaction.- Returns:
- Transaction UID.
-
nodeId
UUID nodeId()
ID of the node on which this transaction started.- Returns:
- Originating node ID.
-
threadId
long threadId()
ID of the thread in which this transaction started.- Returns:
- Thread ID.
-
startTime
long startTime()
Start time of this transaction.- Returns:
- Start time of this transaction on this node.
-
isolation
TransactionIsolation isolation()
Cache transaction isolation level.- Returns:
- Isolation level.
-
concurrency
TransactionConcurrency concurrency()
Cache transaction concurrency mode.- Returns:
- Concurrency mode.
-
implicit
boolean implicit()
Flag indicating whether transaction was started automatically by the system or not. System will start transactions implicitly whenever any cacheput(..)orremove(..)operation is invoked outside of transaction.- Returns:
Trueif transaction was started implicitly.
-
isInvalidate
boolean isInvalidate()
Get invalidation flag for this transaction. If set totrue, then remote values will beinvalidated(set tonull) instead of updated.Invalidation messages don't carry new values, so they are a lot lighter than update messages. However, when a value is accessed on a node after it's been invalidated, it must be loaded from persistent store.
- Returns:
- Invalidation flag.
-
state
TransactionState state()
Gets current transaction state value.- Returns:
- Current transaction state.
-
timeout
long timeout()
Gets timeout value in milliseconds for this transaction. If transaction times out prior to it's completion,TransactionTimeoutExceptionwill be thrown.- Returns:
- Transaction timeout value.
-
timeout
long timeout(long timeout)
Sets transaction timeout value. This value can be set only before a first operation on transaction has been performed.- Parameters:
timeout- Transaction timeout value.- Returns:
- Previous timeout.
-
setRollbackOnly
boolean setRollbackOnly()
Modify the transaction associated with the current thread such that the only possible outcome of the transaction is to roll back the transaction.- Returns:
Trueif rollback-only flag was set as a result of this operation,falseif it was already set prior to this call or could not be set because transaction is already finishing up committing or rolling back.
-
isRollbackOnly
boolean isRollbackOnly()
If transaction was marked as rollback-only.- Returns:
Trueif transaction can only be rolled back.
-
commit
@IgniteAsyncSupported void commit() throws IgniteException
Commits this transaction by initiatingtwo-phase-commitprocess.- Throws:
IgniteException- If commit failed.TransactionTimeoutException- If transaction is timed out.TransactionRollbackException- If transaction is automatically rolled back.TransactionOptimisticException- If transaction concurrency isTransactionConcurrency.OPTIMISTICand commit is optimistically failed.TransactionHeuristicException- If transaction has entered an unknown state.
-
commitAsync
IgniteFuture<Void> commitAsync() throws IgniteException
Asynchronously commits this transaction by initiatingtwo-phase-commitprocess.- Returns:
- a Future representing pending completion of the commit.
- Throws:
IgniteException- If commit failed.TransactionTimeoutException- If transaction is timed out.TransactionRollbackException- If transaction is manually/automatically rolled back.TransactionOptimisticException- If transaction concurrency isTransactionConcurrency.OPTIMISTICand commit is optimistically failed.TransactionHeuristicException- If transaction has entered an unknown state.
-
close
void close() throws IgniteExceptionEnds the transaction. Transaction will be rolled back if it has not been committed.- Specified by:
closein interfaceAutoCloseable- Throws:
IgniteException- If transaction could not be gracefully ended.
-
rollback
@IgniteAsyncSupported void rollback() throws IgniteException
Rolls back this transaction. Note, that it's allowed to roll back transaction from any thread at any time.- Throws:
IgniteException- If rollback failed.
-
rollbackAsync
IgniteFuture<Void> rollbackAsync() throws IgniteException
Asynchronously rolls back this transaction. Note, that it's allowed to roll back transaction from any thread at any time.- Returns:
- a Future representing pending completion of the rollback.
- Throws:
IgniteException- If rollback failed.
-
resume
void resume() throws IgniteExceptionResume a transaction if it was previously suspended.- Throws:
IgniteException- If resume failed.
-
suspend
void suspend() throws IgniteExceptionSuspends a transaction. It could be resumed later.- Throws:
IgniteException- If suspension failed.
-
label
@Nullable @Nullable String label()
Returns transaction's label.Use
IgniteTransactions.withLabel(java.lang.String)to assign a label to a newly created transaction.- Returns:
- Label.
-
-