![](/img/trans.png)
[英]Using Spring @EnableRedisHttpSession with master-slave configuration
[英]Master-slave configuration on spring boot, @Transactional(readOnly = true) not working as expected
所以我有一个主从数据源设置如下:
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.myservice.notificationservice.repositories.happyoffer",
entityManagerFactoryRef = "happyofferEntityManagerFactory",
transactionManagerRef= "happyofferTransactionManager"
)
public class HappyofferDataSourceConfig {
@Value("${spring.entity.scan.packages}")
private String packageToScan;
@Bean
@Primary
@ConfigurationProperties("spring.happyoffer.datasource.master")
public DataSourceProperties happyOfferMasterDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@ConfigurationProperties("spring.happyoffer.datasource.master.configuration")
public DataSource masterDataSource() {
return happyOfferMasterDataSourceProperties().initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
}
@Bean
@ConfigurationProperties("spring.happyoffer.datasource.slave")
public DataSourceProperties happyOfferSlaveDataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "slaveDataSource")
@ConfigurationProperties("spring.happyoffer.datasource.slave.configuration")
public DataSource slaveDataSource() {
return happyOfferSlaveDataSourceProperties().initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
}
@Bean
@Primary
public DataSource routingDataSource() {
Map<Object, Object> targetDataSources = new LinkedHashMap<>();
RoutingDataSourceConfiguration routingDataSourceConfiguration = new RoutingDataSourceConfiguration();
DataSource master = this.masterDataSource();
targetDataSources.put(DataSourceTypes.MASTER, master);
DataSource slave = this.slaveDataSource();
targetDataSources.put(DataSourceTypes.SLAVE, slave);
routingDataSourceConfiguration.setTargetDataSources(targetDataSources);
routingDataSourceConfiguration.setDefaultTargetDataSource(master);
return routingDataSourceConfiguration;
}
@Bean
public DataSource dataSource() {
return new LazyConnectionDataSourceProxy(routingDataSource());
}
@Bean(name = "happyofferEntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean happyofferEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(routingDataSource())
.packages(new String[]{packageToScan})
.build();
}
@Bean
@Primary
public PlatformTransactionManager happyofferTransactionManager(final @Qualifier("happyofferEntityManagerFactory") LocalContainerEntityManagerFactoryBean happyofferEntityManagerFactory) {
return new JpaTransactionManager(happyofferEntityManagerFactory.getObject());
}
@Primary
@Bean(name = "readerJdbcTemplate")
public NamedParameterJdbcTemplate getReaderJdbcTemplate() {
return new NamedParameterJdbcTemplate(slaveDataSource());
}
@Bean(name = "writerJdbcTemplate")
public NamedParameterJdbcTemplate getWriterJdbcTemplate() {
return new NamedParameterJdbcTemplate(masterDataSource());
}
}
路由配置定义为:
public class RoutingDataSourceConfiguration extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
boolean isReadOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly();
if(isReadOnly) {
return DataSourceTypes.SLAVE;
} else {
return DataSourceTypes.MASTER;
}
}
}
我还有一个存储库 class 定义为:
@Repository
@Transactional(readOnly = true)
public interface DeviceInfoRepository extends JpaRepository<DeviceInfo,Integer> {
List<DeviceInfo> findAllByUserIdIn(List<Integer> userIds);
}
我只是在这样的服务中调用上面的 function:
@Service
@Slf4j
public class NotificationShooterServiceImpl implements NotificationShooterService {
@Autowired
DeviceInfoRepository deviceInfoRepository;
@Override
public NotificationShooterResponse shoot(List<Integer> userIds) throws Exception {
List<DeviceInfo> deviceInfoList = deviceInfoRepository.findAllByUserIdIn(userIds);
log.info("Size : " + deviceInfoList.size());
..........
..........
..........
NotificationShooterResponse notificationShooterResponse = new NotificationShooterResponse();
notificationShooterResponse.setCountOfUniqueUserIds(deviceInfoList.size());
return notificationShooterResponse;
}
现在,由于我添加了@Transactional(readOnly = true)
,我希望查询将被路由到 SLAVE 数据库。 但是,我每次都看到它变成了 MASTER。 我对此进行了调试,发现此事务的 readOnly 属性未设置为 true,即在上面显示的文件RoutingDataSourceConfiguration
中, boolean isReadOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly();
读false
。
简而言之,这个问题的解决方案是将hibernate.connection.provider_disables_autocommit=true
添加到application.properties
中,如果使用 Hikari,还可以设置setAutoCommit(false)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.