【发布时间】: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 事务,但如果需要,我想明确创建另一个。我试过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 这样的例子,所以我猜这可能是可行的?
感谢您的帮助。
【问题讨论】: