Transactional Cache Operations

What You Will Learn

How to configure a transactional remote cache and use TransactionManager to group cache operations into transactions that can be committed or rolled back.

Prerequisites

  • Java 17+

  • An Infinispan Server running on localhost:11222 (or Docker/Podman available for Testcontainers)

Start an Infinispan Server with Docker or Podman:

docker run -it --rm -p 11222:11222 -e USER=admin -e PASS=password quay.io/infinispan/server:latest
Tip
You can replace docker with podman in the command above if you use Podman.
Tip
If no server is running, the tutorial code automatically starts an Infinispan Server using Testcontainers.

Step 1: Define a Transactional Cache

Create an XML cache definition with transactional settings in simple-tx-cache.xml:

<distributed-cache name="simple-tx-cache">
    <encoding media-type="application/x-protostream"/>
    <locking isolation="REPEATABLE_READ"/>
    <transaction locking="PESSIMISTIC" mode="NON_XA" />
</distributed-cache>

Step 2: Configure and Connect

Associate the cache with a TransactionManagerLookup and set the transaction mode:

      // Add a transactional cache on startup
      URI cacheConfig = InfinispanRemoteTx.class.getClassLoader().getResource("simple-tx-cache.xml").toURI();
      builder.remoteCache(CACHE_NAME)
              // The cache that will be created is transactional
              .configurationURI(cacheConfig)
              // Use the simple TransactionManager in hot rod client
              .transactionManagerLookup(RemoteTransactionManagerLookup.getInstance())
              // The cache will be enlisted as Synchronization
              .transactionMode(TransactionMode.NON_XA);

      // Connect to the server
      cacheManager = TutorialsConnectorHelper.connect(builder);
      cache = cacheManager.getCache(CACHE_NAME);

Step 3: Commit and Roll Back Transactions

Begin a transaction, put entries, and commit. Then begin another transaction, modify entries, and roll back to discard the changes:

      // Obtain the transaction manager
      TransactionManager transactionManager = cache.getTransactionManager();
      // Perform some operations within a transaction and commit it
      transactionManager.begin();
      cache.put("key1", "value1");
      cache.put("key2", "value2");
      transactionManager.commit();
      // Display the current cache contents
      System.out.printf("key1 = %s\nkey2 = %s\n", cache.get("key1"), cache.get("key2"));

      // Perform some operations within a transaction and roll it back
      // Perform some operations within a transaction and roll it back
      transactionManager.begin();
      cache.put("key1", "value3");
      cache.put("key2", "value4");
      transactionManager.rollback();
      // Display the current cache contents
      System.out.printf("key1 = %s\nkey2 = %s\n", cache.get("key1"), cache.get("key2"));

After rollback, the values remain value1 and value2.

Step 4: Run the Tutorial

mvn package exec:java

You should see:

key1 = value1
key2 = value2
key1 = value1
key2 = value2

The second pair confirms the rollback discarded the changes.

What’s Next