Hazelcast MapStore和JPA信息库

时间:2018-07-20 10:26:41

标签: spring-data-jpa hazelcast

许多天以来,我一直试图使Hazelcast MapStore与JPARepository一起工作,但由于无法创建事务而无法使其工作:

org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress

我知道MapStore不参与文档中所述的Spring事务,但是如果需要,我想显式创建另一个事务。我尝试使用TransactionTemplatePlateformTransactioManager,但似乎没有效果:

  @Autowired
  private UuidSpringDataJpaRepository uuidSpringDataJpaRepository;
  @Autowired
  private PlatformTransactionManager platformTransactionManager;

...

  @Override
  public void storeAll(Map<String, V> map) {
    LOGGER.info("[{}, {}] storeAll: {}", tenant, cacheType, map);
    ClientDatabaseContextHolder.setTenantName(tenant);
    try {
      TransactionTemplate txTemplate = new TransactionTemplate(platformTransactionManager);
      txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
      txTemplate.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
          for (Entry<String, V> entry : map.entrySet()) {
            storeUuidEntity(entry.getKey(), entry.getValue());
          }
          uuidSpringDataJpaRepository.flush();
        }
      });
    } finally {
      ClientDatabaseContextHolder.removeTenantName();
    }
  }

我已经看到像this这样的例子,所以我猜这可能可行吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

@Jerome,您实际上可以让MapStore参与Spring Transaction。在@Transactional或其他服务和/或任何相关方法上使用UuidSpringDataJpaRepository批注,然后调用该方法。