BigMemory 4.3.10 | Product Documentation | BigMemory Max Configuration Guide | Working with Transactions | Avoiding XA Commit Failures With Atomic Methods
 
Avoiding XA Commit Failures With Atomic Methods
If more than one transaction writes to a cache, it is possible for an XA transaction to fail. In the following example, if a second transaction writes to the same key ("1") and completes its commit first, the commit in the example may fail:
...
myTransactionMan.begin();
Cache fooCache = cacheManager.getCache("Foo");
fooCache.put("1", "Bar");
myTransactionMan.commit();
...
One approach to prevent this type of commit failure is to use one of the atomic put methods, such as Cache.replace():
myTransactionMan.begin();
int val = cache.get(key).getValue(); // "cache" is configured to be transactional.
Element olde = new Element (key, val);
// True only if the element was successfully replaced.
if (cache.replace(olde, new Element(key, val + 1)) {
myTransactionMan.commit();
}
else { myTransactionMan.rollback(); }
Another useful atomic put method is Cache.putIfAbsent(Element element), which returns null on success (no previous element exists with the new element's key) or returns the existing element (the put is not executed). Atomic methods cannot be used with null elements, or elements with null keys.