gpt4 book ai didi

java - 配置多个数据源时发现 IllegalTransactionStateException 预绑定(bind) JDBC 连接

转载 作者:行者123 更新时间:2023-12-02 10:37:59 31 4
gpt4 key购买 nike

我有一个 Spring Batch 项目,其中我从数据源读取数据,处理数据并写入另一个主数据源。我正在为 dao 操作扩展 CrudRepository

我正在尝试为我的 springbatch + spring boot 应用程序配置多个数据源,下面是包结构:

myproject
---com
---batch
---config
---firstDsConfig.java
---secondDsConfig.java
---firstrepository
---firstCrudRepository.java
---secondRepository
---SecondCrudRepository.java
---firstEntity
---firstDBEntity.java
---secondEntity
---secondDBEntity.java
----main
---MyMainClass.java

firstDsConfig.java 代码:

@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "firstEntityManagerFactory",
transactionManagerRef = "firstTransactionManager",
basePackages = "com.batch.firstrepository"
)
@EnableTransactionManagement
public class FirstDbConfig {

@Primary
@Bean(name = "firstEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean firstEntityManagerFactory(final EntityManagerFactoryBuilder builder,
final @Qualifier("firstDs") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.batch.firstEntity")
.persistenceUnit("abc")
.build();
}


@Primary
@Bean(name = "firstTransactionManager")
public PlatformTransactionManager firstTransactionManager(@Qualifier("firstEntityManagerFactory")
EntityManagerFactory firstEntityManagerFactory) {
return new JpaTransactionManager(firstEntityManagerFactory);
}


@Primary
@Bean(name = "firstDs")
@ConfigurationProperties(prefix = "spring.datasource.first")
public DataSource firstDataSource() {
return DataSourceBuilder.create().build();
}
}

第二个DsConfig的代码:

@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "secondEntityManagerFactory",
transactionManagerRef = "secondTransactionManager",
basePackages = "com.batch.secondrepository"
)
@EnableTransactionManagement
public class FirstDbConfig {

@Primary
@Bean(name = "secondEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean secondEntityManagerFactory(final EntityManagerFactoryBuilder builder,
final @Qualifier("secondDs") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.batch.secondEntity")
.persistenceUnit("xyz")
.build();
}


@Primary
@Bean(name = "secondTransactionManager")
public PlatformTransactionManager secondTransactionManager(@Qualifier("secondEntityManagerFactory")
EntityManagerFactory firstEntityManagerFactory) {
return new JpaTransactionManager(secondEntityManagerFactory);
}


@Primary
@Bean(name = "secondDs")
@ConfigurationProperties(prefix = "spring.datasource.second")
public DataSource secondDataSource() {
return DataSourceBuilder.create().build();
}
}

这是我的主课

@EnableScheduling
@EnableBatchProcessing
@SpringBootApplication(scanBasePackages = { "com.batch" })
public class MyMainClass {

@Autowired
private JobLauncher jobLauncher;

@Autowired
private JobRepository jobRepository;

@Autowired
@Qualifier(firstDs)
private DataSource dataSource;

@Autowired
@Qualifier("myJob")
private Job job;

public static void main(String[] args) throws Exception {
SpringApplication.run(MyMainClass.class, args);
}

@EventListener(ApplicationReadyEvent.class)
private void start() throws Exception {

jobLauncher.run(job, new JobParameters());
}


@Bean(name="jobService")
public JobService jobService() throws Exception {
SimpleJobServiceFactoryBean factoryBean = new SimpleJobServiceFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setJobRepository(jobRepository);
factoryBean.setJobLocator(new MapJobRegistry());
factoryBean.setJobLauncher(jobLauncher);
factoryBean.afterPropertiesSet();
return factoryBean.getObject();
}
}

这是原始存储库:

第一个凝乳 repo

public interface FirstCrudRepository extends CrudRepository<FirstDbEntity, Integer> {

List<FirstDbEntity> findByOId(String oId);

}

第二个凝乳 repo

public interface SecondCrudRepository extends CrudRepository<SecondDbEntity, Integer> {

List<SecondDbEntity> findByPid(String pid);

}

当我运行应用程序时,在使用 FirstCrudRepository 保存记录时看到以下错误:

 org.springframework.transaction.IllegalTransactionStateException: Pre-bound JDBC Connection found! JpaTransactionManager does not support running within DataSourceTransactionManager if told to manage the DataSource itself. It is recommended to use a single JpaTransactionManager for all transactions on a single DataSource, no matter whether JPA or JDBC access.

注意:我能够从 SecondCrudRepository 成功获取详细信息

最佳答案

默认情况下,如果您提供 DataSource,Spring Batch 将使用 DataSourceTransactionManager,它对您的 JPA 配置一无所知。您需要告诉 Spring Batch 使用您的 JpaTransactionManager。这在以下内容中进行了解释:

关于java - 配置多个数据源时发现 IllegalTransactionStateException 预绑定(bind) JDBC 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53138583/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com