2

我解释上下文:

我实现了来自两个不同数据库的导出和导入记录,换句话说,刷新了七个表。我使用JdbcCursorItemReader执行选择分块查询,因为每个表有超过 600000 条记录,并使用 jdbc 批量插入将每个块保存在另一个 DB 上。实现的想法如下:

REMARK on requirements : 所有表必须在唯一事务中刷新,如果任何表中只有一个块回滚失败

我创建了一种独特的方法,可以在一个独特的事务中为每个表清除、导出/导入块。

带注释的类

@Transactional

我需要同时执行相同的逻辑来清除和填充使用 ConcurrentHashMap 实现的缓存

简而言之:

1) OPEN transaction
   1.1) purge table
   1.2) purge cache
   1.3) read data chunk from DataBase Oracle (1000 records at a time)
   1.4) insert data chunk on DataBase Postgresql (1000 records at a time)
   1.5) put data chunk in cache (ConcurrentHashMap 1000 records at a time)
2) if there aren't exceptions COMMIT all, otherwise ROLLBACK all for DB but also for
   ConcurrentHashMap 
3) CLOSE transaction 

问题是:

Transaction 注释是否也能够应用逻辑提交或回滚 ConcurrentHashMap ?

谢谢

4

2 回答 2

2

EHCache 2.4+ 使用 Spring 的@Transactional注解。

于 2020-03-24T16:45:58.863 回答
2

我做了一个简单的测试来了解实现是否正常。如果没有@Transactional注释,它可以在以下配置中工作:

导入的依赖项:

implementation group: 'org.ehcache', name: 'ehcache', version: '3.0.0'
// https://mvnrepository.com/artifact/org.codehaus.btm/btm
testImplementation group: 'org.codehaus.btm', name: 'btm', version: '2.1.4'

配置类:

import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.transactions.xa.configuration.XAStoreConfiguration;
import org.ehcache.transactions.xa.txmgr.btm.BitronixTransactionManagerLookup;
import org.ehcache.transactions.xa.txmgr.provider.LookupTransactionManagerProviderConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import bitronix.tm.BitronixTransactionManager;
import bitronix.tm.TransactionManagerServices;


@Configuration
public class CacheConfig {


    @Bean(value = "bitronixTransactionManager")
    public BitronixTransactionManager bitronixTransactionManager() {
        BitronixTransactionManager transactionManager =  TransactionManagerServices.getTransactionManager();

        return transactionManager;
    }


    @Bean(value = "cacheSreManager")
    public CacheManager buildCacheManager() {

        BitronixTransactionManager transactionManager = TransactionManagerServices.getTransactionManager();
        CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class)) 
                .withCache("xaCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, 
                                                                    ResourcePoolsBuilder.heap(10)) 
                    .add(new XAStoreConfiguration("xaCache")) 
                    .build()
                )
                .build(true);
        return cacheManager;
    }

}

测试班

@Service("sreEvolutionService")
public class SreEvolutionServiceImpl implements SreEvolutionService {

    @Autowired
    @Qualifier("sreDao")
    private SreDao sreDao;

    @Autowired
    @Qualifier("cacheSreManager")
    private CacheManager cacheManager;

    @Autowired
    @Qualifier("bitronixTransactionManager")
    private BitronixTransactionManager btx;


    @Override
    public void testTxCache() throws Exception {
        Cache<String, String> xaCache  = cacheManager.getCache("xaCache", String.class, String.class);
        try {
            addCache();

            System.out.println(xaCache.get("test2"));
        } catch (Exception e) {
            e.printStackTrace();
            xaCache.get("test2");
        }


    }   

    public void addCache() throws Exception{

        btx.begin();
        Cache<String, String> xaCache  = cacheManager.getCache("xaCache", String.class, String.class);
        xaCache.put("test2", "test2");
        if (1==1) {
            btx.rollback();
            throw new Exception("error test for cache tx");

        }

        btx.commit();
    }

}

缓存有效,但我想@Transactional在方法上实现注释,提前说明我已经为两个不同的数据库实例化了两个事务 bean。

你知道我在哪里可以找到实现示例吗?

于 2020-03-26T07:26:04.027 回答