Search code examples
spring-data-jpahazelcast

Hazelcast MapStore and JPA Repository


Since many days, I try to make an Hazelcast MapStore works with a JPARepository but I cannot make it working because I cannot create a transaction:

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

I know that the MapStore does not participate to the Spring transaction as stated in the documentation but I would like to explicitly create another one if needed. I tried with TransactionTemplate or PlateformTransactioManager but it seems that it has no effect:

  @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();
    }
  }

I saw already some exemples like this so this might be feasible I guess ?

Thanks for your help.


Solution

  • @Jerome, you can actually let MapStore participate into Spring Transaction. Use @Transactional annotation on UuidSpringDataJpaRepository or another Service and/or any related method and call that method.