Transactions with the Go Client

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

This tutorial demonstrates how to use Hot Rod transactions with the Go client to commit or rollback cache operations atomically.

Prerequisites

  • A running Infinispan server.

  • Go 1.25 or later.

Running the Example

go run ./transactions

Code Walkthrough

Transactional Cache Configuration

The cache requires REPEATABLE_READ isolation and PESSIMISTIC locking with NON_XA transaction mode.

<distributed-cache name="simple-tx-cache">
    <locking isolation="REPEATABLE_READ"/>
    <transaction locking="PESSIMISTIC" mode="NON_XA"/>
</distributed-cache>

Committing a Transaction

Return nil from the callback to commit.

client.WithTransaction(ctx, "simple-tx-cache", func(tc *hotrod.TxCache) error {
    tc.Put(ctx, []byte("key1"), []byte("value1"))
    tc.Put(ctx, []byte("key2"), []byte("value2"))
    return nil // commit
})

Rolling Back a Transaction

Return an error from the callback to rollback.

client.WithTransaction(ctx, "simple-tx-cache", func(tc *hotrod.TxCache) error {
    tc.Put(ctx, []byte("key1"), []byte("value3"))
    return errors.New("rollback")
})

Expected Output

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