【发布时间】:2017-08-25 21:44:42
【问题描述】:
我正在研究 Hazelcast Transactional Map 的概念证明。为此,我正在编写一个 Spring Boot 应用程序并使用 Atomikos 作为我的 JTA/XA 实现。
此应用必须更新事务映射并通过在同一事务中插入新行来更新数据库表。
我正在使用 JPA / SpringData / Hibernate 来处理数据库。
所以应用程序有一个组件(一个用@Component 注释的 JAVA 类),它有一个名为 agregar() 的方法(添加西班牙语)。这个方法用@Transactional注解(org.springframework.transaction.annotation.Transactional)
该方法必须作为一个单元执行两项任务:首先必须更新从 Hazelcast 实例检索的 TransactionalMap,其次必须使用从 JpaRepository (org.springframework.data.jpa.repository.JpaRepository) 扩展的存储库更新数据库表
这是我写的代码:
@Transactional
public void agregar() throws NotSupportedException, SystemException, IllegalStateException, RollbackException, SecurityException, HeuristicMixedException, HeuristicRollbackException, SQLException {
logger.info("AGRENADO AL MAPA ...");
HazelcastXAResource xaResource = hazelcastInstance.getXAResource();
UserTransactionManager tm = new UserTransactionManager();
tm.begin();
Transaction transaction = tm.getTransaction();
transaction.enlistResource(xaResource);
TransactionContext context = xaResource.getTransactionContext();
TransactionalMap<TaskKey, TaskQueue> mapTareasDiferidas = context.getMap("TAREAS-DIFERIDAS");
TaskKey taskKey = new TaskKey(1L);
TaskQueue taskQueue = mapTareasDiferidas.get(taskKey);
Integer numero = 4;
Task<Integer> taskFactorial = new TaskImplFactorial(numero);
taskQueue = new TaskQueue();
taskQueue.getQueue().add(taskFactorial);
mapTareasDiferidas.put(taskKey, taskQueue);
transaction.delistResource(xaResource, XAResource.TMSUCCESS);
tm.commit();
logger.info("AGRENADO A LA TABLA ...");
PaisEntity paisEntity = new PaisEntity(100, "ARGENTINA", 10);
paisRepository.save(paisEntity);
}
此代码正在运行:如果其中一个任务引发异常,则两个任务都会回滚。
我的问题是:
- 这段代码真的正确吗?
- 为什么 @Transactional 不负责在地图中提交更改,而我必须明确地自己完成?
项目的完整代码在 Github 上:https://github.com/diegocairone/hazelcast-maps-poc
提前致谢
【问题讨论】:
-
你使用哪个版本的 Hazelcast?
-
Hazelcast 3.6.7 嵌入 Spring Boot 1.4.5
标签: spring-boot hazelcast spring-transactions jta atomikos