Transactions with the .NET Client

Tip
The source code for this tutorial is available on GitHub.

This tutorial demonstrates how to use client-side transactions with the .NET Hot Rod client, including commit and rollback.

Prerequisites

  • A running Infinispan server.

  • .NET 10 or later.

Running the Example

dotnet run --project transactions

Code Walkthrough

Creating a Transactional Cache

Create a cache with pessimistic locking and NON_XA transaction mode.

const string txCacheConfig = """
    <distributed-cache name="tx-cache">
        <locking isolation="REPEATABLE_READ"/>
        <transaction mode="NON_XA" locking="PESSIMISTIC"/>
    </distributed-cache>
    """;
await admin.GetOrCreateCache("tx-cache", txCacheConfig);

Committing a Transaction

Begin a transaction, put entries, and commit. The entries become visible after commit.

var tx = cache.BeginTransaction();
await tx.Put("key1", "value1");
await tx.CommitAsync();

Rolling Back a Transaction

Begin a transaction, put entries, and roll back. The entries revert to their previous values.

var tx = cache.BeginTransaction();
await tx.Put("key1", "value3");
await tx.RollbackAsync();

Expected Output

After commit: key1 = value1, key2 = value2
Transaction rolled back.
After rollback: key1 = value1, key2 = value2