【问题标题】:Hazelcast MapStore and JPA RepositoryHazelcast MapStore 和 JPA 存储库
【发布时间】:2018-07-21 04:05:20
【问题描述】:

很多天以来,我尝试让 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 这样的例子,所以我猜这可能是可行的?

感谢您的帮助。

【问题讨论】:

    标签: spring-data-jpa hazelcast


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      相关资源
      最近更新 更多