Show / Hide Table of Contents

# Interface ITransaction

Grid cache transaction.

Cache transactions support the following isolation levels:

- ReadCommitted isolation 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.
- RepeatableRead isolation 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 is Pessimistic, then a lock on the key will be acquired prior to accessing the value.
- Serializable isolation 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 with RepeatableRead level. However, in Optimistic mode, if some transactions cannot be serially isolated from each other, then one winner will be picked and the other transactions in conflict will result in `TransactionOptimisticException` being thrown on Java side.

Cache transactions support the following concurrency models:

- Optimistic - in this mode all cache operations are not distributed to other nodes until Commit(). 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 1` completes 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 via `CacheConfiguration.setWriteSynchronizationMode`.

  Note that in this mode, optimistic failures are only possible in conjunction with Serializable isolation level. In all other cases, optimistic transactions will never fail optimistically and will always be identically ordered on all participating Ignite nodes.

- Pessimistic - in this mode a lock is acquired on all cache operations with exception of read operations in ReadCommitted mode. All optional filters passed into cache operations will be evaluated after successful lock acquisition. Whenever Commit() 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.

In addition to standard `CacheAtomicityMode.TRANSACTIONAL` behavior, Ignite also supports a lighter `CacheAtomicityMode.ATOMIC` mode 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 that `CacheAtomicityMode.TRANSACTIONAL` mode is used whenever full `ACID`-compliant transactions are not needed. You can use cache transactions as follows:

    using (var tx = cache.Ignite.GetTransactions().TxStart())
    {
        int v1 = cache<string, int>.Get("k1");

        // Check if v1 satisfies some condition before doing a put.
        if (v1 > 0)
            cache.Put<string, int>("k1", 2);

        cache.Remove("k2");

        // Commit the transaction.
        tx.Commit();
    }

##### Inherited Members

System.IDisposable.Dispose()

###### Namespace: Apache.Ignite.Core.Transactions

###### Assembly: Apache.Ignite.Core.dll

##### Syntax

    public interface ITransaction : IDisposable

### Properties

#### Concurrency

Transaction concurrency mode.

##### Declaration

    TransactionConcurrency Concurrency { get; }

##### Property Value

Type | Description
--- | ---
TransactionConcurrency |

#### Isolation

Transaction isolation level.

##### Declaration

    TransactionIsolation Isolation { get; }

##### Property Value

Type | Description
--- | ---
TransactionIsolation |

#### IsRollbackOnly

Gets a value indicating whether this transaction was marked as rollback-only.

##### Declaration

    bool IsRollbackOnly { get; }

##### Property Value

Type | Description
--- | ---
System.Boolean |

#### Label

Label of current transaction.

##### Declaration

    string Label { get; }

##### Property Value

Type | Description
--- | ---
System.String |

#### NodeId

ID of the node on which this transaction started.

##### Declaration

    Guid NodeId { get; }

##### Property Value

Type | Description
--- | ---
System.Guid | Originating node ID.

#### StartTime

Start time of this transaction on this node.

##### Declaration

    DateTime StartTime { get; }

##### Property Value

Type | Description
--- | ---
System.DateTime |

#### State

Current transaction state.

##### Declaration

    TransactionState State { get; }

##### Property Value

Type | Description
--- | ---
TransactionState |

#### ThreadId

ID of the thread in which this transaction started.

##### Declaration

    long ThreadId { get; }

##### Property Value

Type | Description
--- | ---
System.Int64 |

#### Timeout

Timeout for this transaction. If transaction times out prior to it's completion, an exception will be thrown. System.TimeSpan.Zero for infinite timeout.

##### Declaration

    TimeSpan Timeout { get; }

##### Property Value

Type | Description
--- | ---
System.TimeSpan |

### Methods

#### AddMeta<TV>(String, TV)

Adds a new metadata.

##### Declaration

    void AddMeta<TV>(string name, TV val)

##### Parameters

Type | Name | Description
--- | --- | ---
System.String | name | Metadata name.
TV | val | Metadata value.

##### Type Parameters

Name | Description
--- | ---
TV |

#### Commit()

Commits this transaction.

##### Declaration

    void Commit()

#### CommitAsync()

Commits this transaction.

##### Declaration

    Task CommitAsync()

##### Returns

Type | Description
--- | ---
System.Threading.Tasks.Task |

#### Meta<TV>(String)

Gets metadata by name.

##### Declaration

    TV Meta<TV>(string name)

##### Parameters

Type | Name | Description
--- | --- | ---
System.String | name | Metadata name.

##### Returns

Type | Description
--- | ---
TV | Metadata value.

##### Type Parameters

Name | Description
--- | ---
TV |

##### Exceptions

Type | Condition
--- | ---
System.Collections.Generic.KeyNotFoundException | If metadata key was not found.

#### RemoveMeta<TV>(String)

Removes metadata by name.

##### Declaration

    TV RemoveMeta<TV>(string name)

##### Parameters

Type | Name | Description
--- | --- | ---
System.String | name | Metadata name.

##### Returns

Type | Description
--- | ---
TV | Value of removed metadata or default value for `V` type.

##### Type Parameters

Name | Description
--- | ---
TV |

#### Rollback()

Rolls back this transaction.

##### Declaration

    void Rollback()

#### RollbackAsync()

Rolls back this transaction.

##### Declaration

    Task RollbackAsync()

##### Returns

Type | Description
--- | ---
System.Threading.Tasks.Task |

#### SetRollbackonly()

Modify the transaction associated with the current thread such that the only possible outcome of the transaction is to roll back the transaction.

##### Declaration

    bool SetRollbackonly()

##### Returns

Type | Description
--- | ---
System.Boolean | True if rollback-only flag was set as a result of this operation, false if it was already set prior to this call or could not be set because transaction is already finishing up committing or rolling back.