我试图在spring批处理应用程序中配置两个数据源。一个用于批处理元数据表,另一个用于业务表。
my application.properties文件中的代码段:
# primary datasource (Azure SQL server)
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=
# batch datasource(Azure SQL Server)
spring.batchdatasource.url=
spring.batchdatasource.username=
spring.batchdatasource.password=
spring.batchdatasource.driver-class-name=
数据源配置类:
@Configuration
public class DataSourceConfig {
@Bean(name = "batchDatasource")
@ConfigurationProperties(prefix="spring.batchdatasource")
public DataSource batchDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "primaryDatasource")
@ConfigurationProperties(prefix="spring.datasource")
@Primary
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
}
springbatchconfig类:
@Configuration
public class SpringBatchConfig extends DefaultBatchConfigurer{
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Qualifier("batchDataSource")
@Autowired
private DataSource batchDataSource;
@Autowired
private PlatformTransactionManager transactionManager;
@Override
public JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(batchDataSource);
factory.setTransactionManager(transactionManager);
factory.setTablePrefix("schema1"+ ".BATCH_");
factory.afterPropertiesSet();
return factory.getObject();
}
/* step and job beans here */
}
我的主课是用 @EnableBatchProcessing
```
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchExample1Application {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
}
我面临以下例外:
Error creating bean with name 'springBatchConfig': Invocation of init method failed; nested exception is org.springframework.batch.core.configuration.BatchConfigurationException: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
基于此答案,我将url属性名称更改如下:
spring.datasource.jdbc-url=
spring.batchdatasource.jdbc-url=
虽然它解决了jdbcurl错误,但它带来了另一个问题:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Reference to database and/or server name in 'sample-sql-server.schema1.MY_TABLE_NAME' is not supported in this version of SQL Server.
我的两个数据源都是azure sql server示例。我查了一下,发现几年前不可能使用多个azure sql数据库,但基于这个问题,情况应该不再如此了。
暂无答案!
目前还没有任何答案,快来回答吧!