弹簧启动上的主从配置,@Transactional(只读 = true)未按预期工作



所以我有一个主从数据源设置如下:

@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;
}
}
}

我还有一个存储库类定义为:

@Repository
@Transactional(readOnly = true)
public interface DeviceInfoRepository extends JpaRepository<DeviceInfo,Integer> {
List<DeviceInfo> findAllByUserIdIn(List<Integer> userIds);
}

我只是在这样的服务中调用上述函数:

@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 db。但是,我每次都看到它都会成为大师。 我对此进行了调试,发现此事务的 readOnly 属性未设置为 true,即 在上面显示的文件RoutingDataSourceConfiguration中,boolean isReadOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly();false.

简而言之,此问题的解决方案是将hibernate.connection.provider_disables_autocommit=true添加到application.properties,如果使用Hikari,还可以设置setAutoCommit(false)

更多细节可以在这里找到

最新更新