Transactions Example

Error formatting macro: composition-setup: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil

You can group cache updates with GemFire transactions. Transactions allow you to create dependencies between cache modifications so they all either complete together or fail together. You can execute transactions against the data in the distributed cache similar to the way you execute transactions in a database, with the standard begin, commit, and rollback operations. GemFire transactions are compatible with Java Transaction API (JTA) transactions and, when run inside a J2EE container, are also compatible with the container's JNDI and JTA transaction manager. Read more about transactions.

Running the Example

This example runs and commits one transaction and then runs and rolls back another.

To run this example, you must have terminal sessions configured for the QuickStart examples, as described in Setting Up the Environment.

In a single session, run the example:

$ java quickstart.Transactions

Example Listings

Program and cache configuration file listings:

Error formatting macro: deck: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
<?xml version="1.0"?>
<!DOCTYPE cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
    "http://www.gemstone.com/dtd/cache6_5.dtd">
    
<!--
  | Transactions.xml
  |
  | Configures a simple distributed region. 
 -->
<cache>
	<region name="exampleRegion">
		<region-attributes scope="distributed-no-ack" />
	</region>
</cache>
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
package quickstart;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Set;
import java.util.TreeSet;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.CacheTransactionManager;
import com.gemstone.gemfire.cache.Region;

/**
 * You can execute transactions against the data in the distributed cache as 
 * you would in a database. GemFire transactions are compatible with Java 
 * Transaction API (JTA) transactions. Please refer to the quickstart guide 
 * for instructions on how to run this example. 
 * 
 * @author GemStone Systems, Inc.
 * @since 4.1.1
 */
public class Transactions {
	
  public static void main(String[] args) throws Exception {
    System.out.println("\nThis example demonstrates transactions on a GemFire cache.");
    System.out.println("\nConnecting to the distributed system and creating the cache.");
    
    // Create the cache which causes the cache-xml-file to be parsed
    Cache cache = new CacheFactory()
      .set("name", "Transactions")
      .set("cache-xml-file", "xml/Transactions.xml")
      .create();

    // Get the exampleRegion
    Region<String,String> exampleRegion = cache.getRegion("exampleRegion");
    System.out.println("Example region, " + exampleRegion.getFullPath() + ", created in cache. ");

    // Get JNDI(Java Naming and Directory interface) context
    CacheTransactionManager tx = cache.getCacheTransactionManager();
    
    // Print the data initial data in the region.
    printRegionData(exampleRegion);
    
    // Begin transaction that I will commit...
    System.out.println("\nBefore the first transaction, the cache is empty.\n");
    
    tx.begin();
    for (int count = 0; count < 3; count++) {
      String key = "key" + count;
      String value =  "CommitValue" + count;
      System.out.println("Putting entry: " + key + ", " + value);
      exampleRegion.put(key, value);
    }
    tx.commit();
    
    System.out.println("\nAfter committing the first transaction, the cache contains:");
    printRegionData(exampleRegion);

    System.out.println("\nPress Enter to continue to next transaction...");
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    bufferedReader.readLine();
    
    // Begin a transaction which I will rollback...
    System.out.println("Before the second transaction, the cache contains:");
    printRegionData(exampleRegion);
    System.out.println("");
    
    tx.begin();
    for (int count = 0; count < 3; count++) {
      String key = "key" + count;
      String value =  "RollbackValue" + count;
      System.out.println("Putting entry: " + key + ", " + value);
      exampleRegion.put(key, value);
    }
    tx.rollback();
    
    System.out.println("\nAfter rolling back the second transaction, the cache contains:");
    printRegionData(exampleRegion);
    
    // Close the cache and disconnect from GemFire distributed system
    System.out.println("\nClosing the cache and disconnecting.");
    cache.close();
  }
  
  private static void printRegionData(Region<String,String> exampleRegion) {
    Set<String> keySet = new TreeSet<String>(exampleRegion.keySet());
    for (String entryKey: keySet) {
      Region.Entry<String,String> entry = exampleRegion.getEntry(entryKey);
      String entryValue = entry.getValue();
      System.out.println("        entry: " + entryKey + ", " + entryValue);
    }
  }
}

Related Javadocs

CacheTransactionManager, TransactionEvent, TransactionListener

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.