The Problem
You want to update your distributed cache, but only if you succeed in other operations like updating a DBMS or sending a message to a queue.
The Solution
Use our Hazelcast integration to update the cache, and the outcome will be automatically subject to commit / rollback of any JTA transactions in your application.
Code sample
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.TransactionalMap;
import com.hazelcast.instance.HazelcastInstanceFactory;
import com.hazelcast.transaction.TransactionContext;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostContruct;
import com.atomikos.hazelcast.AtomikosHazelcastInstanceFactory;
@Transactional
public class BlogPostDemoTest {
private HazelcastInstance jtaAwareInstance; // this instance will make Hazelcast transactional operations part of JTA
@PostConstruct public void init() {
HazelcastInstance nativeInstance = HazelcastInstanceFactory.newHazelcastInstance(...); // create native instance
jtaAwareInstance = AtomikosHazelcastInstanceFactory.createJtaAwareHazelcastInstance("hazelcast", nativeInstance);
}
public void updateCacheInJta() {
TransactionContext ctx = jtaAwareInstance.newTransactionContext(); // can be called multiple times inside the same JTA transaction
final TransactionalMap<Object, Object> map = ctx.getMap("map");
map.put("key", "value");
}
}

Add a comment