Hazelcast MapStore 和 JPA 存储库
Hazelcast MapStore and JPA Repository
很多天以来,我尝试使 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 事务,但我想在需要时明确创建另一个事务。我试过 TransactionTemplate
或 PlateformTransactioManager
但似乎没有效果:
@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 这样的例子,所以我猜这可能是可行的?
感谢您的帮助。
@Jerome,您实际上可以让 MapStore 参与到 Spring 事务中。在 UuidSpringDataJpaRepository
或其他服务 and/or 任何相关方法上使用 @Transactional
注释并调用该方法。
很多天以来,我尝试使 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 事务,但我想在需要时明确创建另一个事务。我试过 TransactionTemplate
或 PlateformTransactioManager
但似乎没有效果:
@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 这样的例子,所以我猜这可能是可行的?
感谢您的帮助。
@Jerome,您实际上可以让 MapStore 参与到 Spring 事务中。在 UuidSpringDataJpaRepository
或其他服务 and/or 任何相关方法上使用 @Transactional
注释并调用该方法。