Code Samples

Like thread local caches, transaction local caches should only by accessed where the correct context (transaction) exists.

Figure 1. Setting up and using a transaction local cache
public void myMethod() {
  ...
  Cache<String, String> txnCache = CacheManagerEjb.
           getTransactionLocalCacheGroup().getCache("mycache");
  String value = txnCache.get("key");
  if(value == null) {
    // perform expensive operation to calculate value - this
    // processing only happens once per transaction
    ...
    // and store the result
    txnCache.put("key", "value");
  }
  ...
}