【问题标题】:TaskConfigurer does not considering schema name for datasourceTaskConfigurer 不考虑数据源的架构名称
【发布时间】:2018-04-26 05:15:33
【问题描述】:

我有 2 个数据源。对于一个数据源,我想使用自定义模式名称。出于这个原因,我将我的数据源网址设置为 spring.datasource.url=jdbc:postgresql://192.168.33.10/analytics?currentSchema=bahmni_mart_scdf。 但它在 public 架构中创建所有表,而不是 bahmni_mart_scdf 架构。

我已经重写了 TaskRepositoryInitializer bean 并实现了 TaskConfigurer。

这是我的实现

DatabaseConfiguration.class

@Configuration
public class DatabaseConfiguration {
    @Bean(name = "martDb")
    @ConfigurationProperties(prefix = "spring.ds_mart")
    public DataSource martDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "martJdbcTemplate")
    public JdbcTemplate martJdbcTemplate(@Qualifier("martDb") DataSource dsMart) {
        return new JdbcTemplate(dsMart);
    }

    @Bean(name = "scdfDb")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "scdfJdbcTemplate")
    public JdbcTemplate scdfJdbcTemplate(@Qualifier("scdfDb") DataSource scdfDb) {
        return new JdbcTemplate(scdfDb);
    }
}

DataloadTaskConfigurer.class

@Component
public class DataloadTaskConfigurer implements TaskConfigurer {
    private DataSource dataSource;

    private TaskRepository taskRepository;

    private TaskExplorer taskExplorer;

    private PlatformTransactionManager transactionManager;

    @Autowired
    public DataloadTaskConfigurer(@Qualifier("scdfJdbcTemplate") JdbcTemplate jdbcTemplate) {

        this.dataSource = jdbcTemplate.getDataSource();
        TaskExecutionDaoFactoryBean taskExecutionDaoFactoryBean = new TaskExecutionDaoFactoryBean(dataSource);
        this.taskRepository = new SimpleTaskRepository(taskExecutionDaoFactoryBean);
        this.taskExplorer = new SimpleTaskExplorer(taskExecutionDaoFactoryBean);
    }

    @Override
    public TaskRepository getTaskRepository() {
        return this.taskRepository;
    }

    @Override
    public PlatformTransactionManager getTransactionManager() {
        if (this.transactionManager == null) {
            this.transactionManager = new DataSourceTransactionManager(this.dataSource);
        }
        return this.transactionManager;
    }

    @Override
    public TaskExplorer getTaskExplorer() {
        return this.taskExplorer;
    }

    public DataSource getTaskDataSource() {
        return this.dataSource;
    }
}

TaskConfiguration.class

@Configuration
public class TaskConfiguration {

    @Bean
    public TaskRepositoryInitializer taskRepositoryInitializerInDataMart(@Qualifier("scdfJdbcTemplate") JdbcTemplate jdbcTemplate) throws Exception {
        TaskRepositoryInitializer taskRepositoryInitializer = new TaskRepositoryInitializer();
        taskRepositoryInitializer.setDataSource(jdbcTemplate.getDataSource());
        taskRepositoryInitializer.afterPropertiesSet();
        return taskRepositoryInitializer;
    }
}

application.properties

spring.datasource.url=jdbc:postgresql://192.168.33.10/analytics?currentSchema=bahmni_mart_scdf
spring.datasource.username=analytics
spring.datasource.password=""
spring.datasource.driver-class-name=org.postgresql.Driver

spring.ds_mart.url=jdbc:postgresql://192.168.33.10/analytics
spring.ds_mart.username=analytics
spring.ds_mart.password=""
spring.ds_mart.driver-class-name=org.postgresql.Driver

【问题讨论】:

    标签: java spring postgresql spring-boot spring-cloud-task


    【解决方案1】:

    您不需要覆盖TaskRepositoryInitializer,而是通过spring.cloud.task.initialize.enable=false 将其全部关闭。从那里,使用 Spring Boot,您需要传入每个数据源的模式:

    spring.datasource.schema=schema1.sql
    spring.ds_mart.schema=schema2.sql
    

    【讨论】:

      【解决方案2】:

      问题出在 postgresql 驱动程序版本上。一旦我更新到最新版本(42.2.2),一切都会按预期正常运行

      【讨论】:

        猜你喜欢
        • 2018-07-30
        • 1970-01-01
        • 1970-01-01
        • 2015-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-22
        相关资源
        最近更新 更多