在配置多个数据源时,我正在尝试使用jpa截断表,该命令似乎已提交,但实际上-表中的数据仍然存在并且未被删除。
我的代码段如下:
相关数据源配置
@Configuration
@EnableJpaRepositories(basePackages = "com.haaretz.jobs.newsletter_preferences_job.data.newsletter_alerts")
public class NewsletterAlertsConfig {
private Environment environment;
@Autowired
public NewsletterAlertsConfig(Environment environment){
this.environment = environment;
}
@Primary
@Bean(name = "newsletterAlertsDataSource")
public DataSource newsletterAlertsDataSource() {
return DataSourceBuilder
.create()
.driverClassName(environment.getProperty("datasource.newsletterAlerts.driverClassName"))
.url(environment.getProperty("datasource.newsletterAlerts.jdbcUrl"))
.username(environment.getProperty("datasource.newsletterAlerts.username"))
.password(environment.getProperty("datasource.newsletterAlerts.password"))
.build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("newsletterAlertsDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.haaretz.jobs.newsletter_preferences_job.data.newsletter_alerts")
.persistenceUnit("newsletterAlerts")
.build();
}
}
第二个数据源已配置
@Configuration
@EnableJpaRepositories(basePackages = "com.haaretz.jobs.newsletter_preferences_job.data.gstat",
entityManagerFactoryRef = "gstatEntityManagerFactory")
public class GstatConfig {
private Environment environment;
@Autowired
public GstatConfig(Environment environment){
this.environment = environment;
}
@Bean(name = "gstatDataSource")
public DataSource gstatDataSource() {
return DataSourceBuilder
.create()
.driverClassName(environment.getProperty("datasource.gstat.driverClassName"))
.url(environment.getProperty("datasource.gstat.jdbcUrl"))
.username(environment.getProperty("datasource.gstat.username"))
.password(environment.getProperty("datasource.gstat.password"))
.build();
}
@Bean(name = "gstatEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean gstatEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("gstatDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.haaretz.jobs.newsletter_preferences_job.data.gstat")
.persistenceUnit("gstat")
.build();
}
}
存储库
@Repository
public interface NewsletterAlertsRepository extends CrudRepository<NewsletterOpPersonalNl, String> {
@Modifying
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Query(
value = "truncate table OP_PERSONAL_NL",
nativeQuery = true
)
void truncate();
}
激活删除的作业
@Component
public class NewsletterPreferencesJob {
private NewsletterAlertsRepository newsletterAlertsRepository;
@Autowired
public NewsletterPreferencesJob(NewsletterAlertsRepository newsletterAlertsRepository) {
this.newsletterAlertsRepository = newsletterAlertsRepository;
}
@Transactional
public void doTask() {
newsletterAlertsRepository.truncate();
}
}
我还尝试过使用crudRepoditory默认函数,该函数返回相同的结果(已提交但未实际删除):
newsletterAlertsRepository.deleteAll();
或使用事务管理器,实际上也不会删除数据
TransactionTemplate transactionTemplate = new TransactionTemplate(platformTransactionManager);
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
entityManager.createNativeQuery("truncate table OP_PERSONAL_NL").executeUpdate();
}
});