Codebeispiele

Wie auf lokale Caches für Threads darf auch auf lokale Caches für Transaktionen nur zugegriffen werden, wenn der korrekte Kontext (die korrekte Transaktion) verwendet wird.

Abbildung 1. Lokalen Cache für eine Transaktion konfigurieren und verwenden
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");
  }
  ...
}