我需要使用Transactional注释通过SpringTransactionManager实现缓存Ignite的事务。这对于指示交易和审计的方面的执行顺序是必要的。 这就是我做到的。
IgniteConfig:
@EnableTransactionManagement
@Configuration
public class IgniteConfig {
@Value("${ignite.cache.name}")
private String igniteCacheName;
@Autowired
private IgniteIpFinderProvider ipFinderProvider;
@Bean
public Ignite ignite() {
return Ignition.start(igniteConfiguration());
}
private IgniteConfiguration igniteConfiguration() {
IgniteConfiguration igniteConfiguration = new IgniteConfiguration();
igniteConfiguration.setGridName(UUID.randomUUID().toString());
igniteConfiguration.setDiscoverySpi(getDiscoverySpi());
igniteConfiguration.setCacheConfiguration(getCacheConfig());
return igniteConfiguration;
}
@Bean
@Qualifier(value = "transactionManager")
public SpringTransactionManager transactionManager() {
SpringTransactionManager transactionManager = new SpringTransactionManager();
transactionManager.setConfiguration(igniteConfiguration());
return transactionManager;
}
private DiscoverySpi getDiscoverySpi() {
TcpDiscoverySpi discoverySpi = new TcpDiscoverySpi();
discoverySpi.setIpFinder(ipFinderProvider.getIpFinder());
return discoverySpi;
}
private CacheConfiguration getCacheConfig() {
CacheConfiguration<String, String> cacheConfiguration = new CacheConfiguration<>();
cacheConfiguration.setName(igniteCacheName);
cacheConfiguration.setIndexedTypes(String.class, String.class);
cacheConfiguration.setCacheMode(CacheMode.PARTITIONED);
cacheConfiguration.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
cacheConfiguration.setBackups(2);
return cacheConfiguration;
}
}
存储库:
@Transactional(transactionManager = "transactionManager", rollbackForClassName = "Exception")
public String putAndGet(String userName, String userSessionData) {
ignite.<String, String>cache(igniteCacheName).put(userName, userSessionData);
method();
return ignite.<String, String>cache(igniteCacheName).get(userName);
}
void method() throws CacheException {
throw new CacheException();
}
但问题是回滚操作不起作用。
谁知道如何在Ignite中使用Spring Transaction实现事务?
我很乐意提供任何帮助!