我通过实现redis缓存,使用jhipster构建了一个单片spring引导项目。我尝试使用符号将redis事务与spring事务结合起来。我正在尝试使用redisson-spring事务管理器类来实现这一点。然而,当我尝试注册一个人时,mysql数据库中没有创建数据,redis中有个人。
我不知道我做错了什么配置。
@Configuration
@EnableTransactionManagement
public class RedisTxConfig {
@Bean
public RedissonTransactionManager transactionManager(RedissonClient redisson) {
return new RedissonTransactionManager(redisson);
}
}
.
@Configuration
@EnableCaching
public class CacheConfiguration {
private GitProperties gitProperties;
private BuildProperties buildProperties;
@Bean
public javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration(JHipsterProperties jHipsterProperties) {
MutableConfiguration<Object, Object> jcacheConfig = new MutableConfiguration<>();
URI redisUri = URI.create(jHipsterProperties.getCache().getRedis().getServer()[0]);
Config config = new Config();
if (jHipsterProperties.getCache().getRedis().isCluster()) {
ClusterServersConfig clusterServersConfig = config
.useClusterServers()
.setMasterConnectionPoolSize(jHipsterProperties.getCache().getRedis().getConnectionPoolSize())
.setMasterConnectionMinimumIdleSize(jHipsterProperties.getCache().getRedis().getConnectionMinimumIdleSize())
.setSubscriptionConnectionPoolSize(jHipsterProperties.getCache().getRedis().getSubscriptionConnectionPoolSize())
.addNodeAddress(jHipsterProperties.getCache().getRedis().getServer());
if (redisUri.getUserInfo() != null) {
clusterServersConfig.setPassword(redisUri.getUserInfo().substring(redisUri.getUserInfo().indexOf(':') + 1));
}
} else {
SingleServerConfig singleServerConfig = config
.useSingleServer()
.setConnectionPoolSize(jHipsterProperties.getCache().getRedis().getConnectionPoolSize())
.setConnectionMinimumIdleSize(jHipsterProperties.getCache().getRedis().getConnectionMinimumIdleSize())
.setSubscriptionConnectionPoolSize(jHipsterProperties.getCache().getRedis().getSubscriptionConnectionPoolSize())
.setAddress(jHipsterProperties.getCache().getRedis().getServer()[0]);
if (redisUri.getUserInfo() != null) {
singleServerConfig.setPassword(redisUri.getUserInfo().substring(redisUri.getUserInfo().indexOf(':') + 1));
}
}
jcacheConfig.setStatisticsEnabled(true);
jcacheConfig.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, jHipsterProperties.getCache().getRedis().getExpiration())));
return RedissonConfiguration.fromInstance(redisson(config), jcacheConfig);
}
@Bean
public JCacheManagerCustomizer cacheManagerCustomizer(javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration) {
return cm -> {
createCache(cm, net.invaco.repository.UserRepository.USERS_BY_LOGIN_CACHE, jcacheConfiguration);
createCache(cm, net.invaco.repository.UserRepository.USERS_BY_EMAIL_CACHE, jcacheConfiguration);
// jhipster-needle-redis-add-entry
};
}
private void createCache(javax.cache.CacheManager cm, String cacheName, javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration) {
javax.cache.Cache<Object, Object> cache = cm.getCache(cacheName);
if (cache == null) {
cm.createCache(cacheName, jcacheConfiguration);
}
}
@Autowired(required = false)
public void setGitProperties(GitProperties gitProperties) {
this.gitProperties = gitProperties;
}
@Autowired(required = false)
public void setBuildProperties(BuildProperties buildProperties) {
this.buildProperties = buildProperties;
}
@Bean
public KeyGenerator keyGenerator() {
return new PrefixedKeyGenerator(this.gitProperties, this.buildProperties);
}
@Bean(destroyMethod="shutdown")
public RedissonClient redisson(Config config) {
return Redisson.create(config);
}
}
.
@Configuration
@EnableJpaRepositories(
basePackages = "com.test.repository")
@EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
@EnableTransactionManagement
public class DatabaseConfiguration {
private final Logger log = LoggerFactory.getLogger(DatabaseConfiguration.class);
@Bean
@Primary
@ConfigurationProperties("spring.datasource")
public DataSourceProperties defaultDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties("spring.datasource")
public DataSource defaultDataSource() {
return defaultDataSourceProperties().initializeDataSourceBuilder().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setPackagesToScan("com.test.domain");
factoryBean.setJpaVendorAdapter(jpaVendorAdapter());
return factoryBean;
}
@Primary
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}
@Bean(name = "transactionManager")
@Primary
public JpaTransactionManager transactionManager(@Qualifier("entityManagerFactory") final EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
}
.
@Service
@Transactional(rollbackFor = Exception.class)
public class TestService {
private final Logger log = LoggerFactory.getLogger(TestService.class);
@Autowired
private PersonService personService;
@Autowired
private RedissonTransactionManager redissonTransactionManager;
public void wrapperMetod1(String name, Integer age) throws Exception {
log.info("Call wrapper metod 1");
TransactionStatus status = TransactionAspectSupport.currentTransactionStatus();
log.warn("transaction1 : {} , {} , {} , {}", status.hashCode(), status.isNewTransaction(), status.toString(), status.getClass());
innerMetod1(name, age);
innerMetod2();
}
public void innerMetod1(String name, Integer age) {
log.info("Call inner metod 1");
TransactionStatus status = TransactionAspectSupport.currentTransactionStatus();
log.warn("transaction2 : {} , {} , {} , {}", status.hashCode(), status.isNewTransaction(), status.toString(), status.getClass());
Person person = new Person(name, age);
Person savedPerson = personService.save(person);
RTransaction transaction = redissonTransactionManager.getCurrentTransaction();
RMap<String, Person> personMap = transaction.getMap("personMap");
personMap.put("10", savedPerson);
}
public void innerMetod2() throws Exception {
log.info("Call inner metod 2");
if (TestResource.ex) {
throw new Exception("test");
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!