Exemples de code

Comme pour les caches d'unité d'exécution locale, vous ne devez accéder aux caches de transaction locale que lorsque le contexte approprié (transaction) existe.

Figure 1. Configuration et utilisation d'un cache de transaction locale
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");
  }
  ...
}