首页 > 解决方案 > 配置多个数据源时,此版本的 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 数据库,但基于这个问题,现在应该不再是这种情况了。

标签: javaspringspring-bootazureazure-sql-database

解决方案


推荐阅读