Apache Ignite org.apache.ignite.transactions.spring.SpringTransactionManagaer不会使用IgniteRepository回滚

时间:2019-06-26 21:03:24

标签: spring transactions spring-data ignite spring-transactions

Apache Ignite SpringTransactionManager不会回滚事务。

点燃2.7.5,弹簧靴2.1.6

实体:

@Data
@AllArgsConstructor
public class CacheEntity {
    @QuerySqlField
    private String uuid;
}

存储库:

@RepositoryConfig(cacheName = CACHE_NAME)
public interface TestRepository extends IgniteRepository<CacheEntity, Long> {

}

测试

@ActiveProfiles(profiles = {"junit"})
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = IgniteRepositoryTransactionTest.Config.class)
public class IgniteRepositoryTransactionTest {
    public static final String CACHE_NAME = "testCache";
    @Autowired
    private Ignite ignite;
    @Autowired
    private SpringTransactionManager ignitePessimisticTransactionManager;
    @Autowired
    private TestRepository repository;


    @Test
    public void txTest() {
        ignite.cache(CACHE_NAME).clear();

        try {
            final TransactionTemplate txTemplate = new TransactionTemplate(ignitePessimisticTransactionManager);
            txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
            txTemplate.execute((TransactionCallback<Void>) status -> {
//                ignite.cache(CACHE_NAME).put(1L, new CacheEntity(UUID.randomUUID().toString()));
                repository.save(1L, new CacheEntity(UUID.randomUUID().toString()));
                throw new RuntimeException("exception during transaction");
            });
        } catch (RuntimeException ignored) {
        }

        assertFalse("not saved", repository.findById(1L).isPresent());
//        assertNull("not saved", ignite.cache(CACHE_NAME).get(1L));
    }

    @TestConfiguration
    @EnableIgniteRepositories(basePackageClasses = {TestRepository.class})
    @EnableTransactionManagement
    public static class Config {

        @Bean
        public IgniteConfiguration igniteConfiguration() {
            IgniteConfiguration icfg = new IgniteConfiguration();
            icfg.setClientMode(false);
            icfg.setIgniteInstanceName(CACHE_NAME + "-instance");
            icfg.setPeerClassLoadingEnabled(true);

            CacheConfiguration<Long, CacheEntity> ccf = new CacheConfiguration<>(CACHE_NAME);
            ccf.setIndexedTypes(Long.class, CacheEntity.class);

            icfg.setCacheConfiguration(ccf);

            return icfg;
        }

        @Bean
        public Ignite igniteInstance() {
            return Ignition.start(igniteConfiguration());
        }

        @Bean
        public SpringTransactionManager ignitePessimisticTransactionManager() {
            final SpringTransactionManager transactionManager = new SpringTransactionManager();
            transactionManager.setIgniteInstanceName(igniteInstance().name());
            transactionManager.setTransactionConcurrency(TransactionConcurrency.PESSIMISTIC);
            transactionManager.setTransactionSynchronization(AbstractPlatformTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);

            return transactionManager;
        }
    }
}

我看到回滚逻辑被调用,并且没有任何异常。但是最终,实体在残破的保存操作期间进行保存。

使用ignite实例代替存储库。

23:54:15.049 [main]调试org.apache.ignite.transactions.spring.SpringTransactionManager-创建名称为[null]的新交易:PROPAGATION_REQUIRED,ISOLATION_DEFAULT 23:54:15.119 [main]调试org.springframework.transaction.support.TransactionTemplate-在应用程序异常时启动事务回滚

更新: 感谢alamar评论。事务缓存模式默认为禁用:

ccf.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);

0 个答案:

没有答案