java - 配置多个数据源时,此版本的 SQL Server 不支持引用数据库和/或服务器名称
问题描述
我正在尝试在我的 spring 批处理应用程序中配置两个数据源。一个用于批处理元数据表,另一个用于业务表。
我的 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 服务器实例。我查了一下,发现几年前不可能使用多个 Azure SQL 数据库,但基于这个问题,现在应该不再是这种情况了。
解决方案
推荐阅读
- python - 使用烧瓶使路由只能在调试模式下访问
- javascript - d3js在多个图表之间切换
- python - python 中的 RegEx 测试正常,但没有正确实施
- azure - 在 Azure 中创建代理
- c++ - 同时运行三个功能?
- java - Sqlite3:如何对查询结果进行 URLEncode?
- java - 如何在 Windows 上使用 Javaw 发生 JNI 错误 Intellij IDEA 在没有控制台窗口的情况下运行 GUI
- sql - 使用 .MDB 文件类型而不是 .ACCDB 文件类型是否理想?
- go - Go:读取文件,goroutine死锁
- python - 每列的线性回归,无需创建 for 循环或函数