Spring boot 使用具有自动配置和非标准数据库的Spring批处理

Spring boot 使用具有自动配置和非标准数据库的Spring批处理,spring-boot,spring-batch,Spring Boot,Spring Batch,我正在尝试创建一个Spring批处理应用程序。我们使用SQLAnywhere数据库,它实际上是一种已知的数据库类型。为了简化操作,我在我的主类上使用了@SpringBootApplication,在配置类上使用了@EnableBatchProcessing 问题在于,我的数据库驱动程序sybase.jdbc4.sqlanywhere.IDriver,返回一个产品名“sqlanywhere”,Spring无法识别该名称,从而导致各种错误。通过在配置类中显式创建JobRepositoryFactor

我正在尝试创建一个Spring批处理应用程序。我们使用SQLAnywhere数据库,它实际上是一种已知的数据库类型。为了简化操作,我在我的主类上使用了
@SpringBootApplication
,在配置类上使用了
@EnableBatchProcessing

问题在于,我的数据库驱动程序sybase.jdbc4.sqlanywhere.IDriver,返回一个产品名“sqlanywhere”,Spring无法识别该名称,从而导致各种错误。通过在配置类中显式创建JobRepositoryFactoryBean,我能够克服其中的一些问题:

/**
 * We can't rely on Spring Boot as it can't set the database type properly.
 * 
 * By explicitly requiring the arguments in the constructor, we force the Autowiring
 * to occur.
 */
@Bean
public JobRepositoryFactoryBean jobRepositoryFactory(DataSource ds, PlatformTransactionManager tm) {
    JobRepositoryFactoryBean jf = new JobRepositoryFactoryBean();

    jf.setDataSource(ds);
    jf.setTransactionManager(tm);
    jf.setDatabaseType("SQLSERVER");
    jf.setTablePrefix("DBA.BATCH_");
    jf.setIsolationLevelForCreate("ISOLATION_SERIALIZABLE");    // only one instance at a time

    return jf;
}
但是,
DefaultBatchConfigurer
在初始化函数中失败,因为它显式地构造了自己的JobExplorerFactoryBean


我想知道是否有一些简单的方法来解决这个问题,或者我是否必须复制
DefaultBatchConfigurer
类中的工作,自己定义所有bean并删除
@EnableBatchProcessing
注释。

我能够解决这个问题,我希望它能帮助任何试图使用SpringBatch和现成数据库的人。有必要扩展
DefaultBatchConfigurer
并重写
createJobRepository()
函数。此外,还应禁用自动创建作业表

以下是我创建的类,它可以用作任何SQL Anywhere Spring批处理作业的基础:

@EnableBatchProcessing
public class SqlAnywhereBatchConfigurer extends DefaultBatchConfigurer {

  @Autowired
  private DataSource dataSource;
  @Autowired
  private PlatformTransactionManager transactionManager;

  public SqlAnywhereBatchConfigurer() {
      super();
  }

  public SqlAnywhereBatchConfigurer(DataSource dataSource) {
      super(dataSource);
  }

  @Override
  protected JobRepository createJobRepository() throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDataSource(dataSource);
    factory.setTransactionManager(transactionManager);
    factory.setDatabaseType("SQLSERVER");
    factory.afterPropertiesSet();
    return factory.getObject();
  }
}
请记住首先使用sqlserver.sql模式设置表

您必须在配置中,在
应用程序中定义数据源。属性
我有:

# database
spring.datasource.url=jdbc:sqlanywhere:Server=<your server name>;port=2638
spring.datasource.username=<your username>
spring.datasource.password=<your password>
spring.datasource.driver-class-name=sybase.jdbc4.sqlanywhere.IDriver
# don't create tables on startup
spring.datasource.initialize=false
spring.batch.initializer.enabled=false
#数据库
url=jdbc:sqlanywhere:Server=;端口=2638
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver类名=sybase.jdbc4.sqlanywhere.IDriver
#不要在启动时创建表
spring.datasource.initialize=false
spring.batch.initializer.enabled=false

最后,这里值得一提的是,对于SQL Anywhere,至少
JdbcCursorItemReader
不起作用,因为设置准备语句的获取方向会导致抛出“不受支持的”
SQL异常。您可以通过扩展
JdbcCursorItemReader
并在您自己的类中重写
applyStatementSettings
函数(加上一些setter)来解决这个问题。

我能够解决这个问题,我希望它能帮助任何试图使用Spring Batch和现成数据库的人。有必要扩展
DefaultBatchConfigurer
并重写
createJobRepository()
函数。此外,还应禁用自动创建作业表

以下是我创建的类,它可以用作任何SQL Anywhere Spring批处理作业的基础:

@EnableBatchProcessing
public class SqlAnywhereBatchConfigurer extends DefaultBatchConfigurer {

  @Autowired
  private DataSource dataSource;
  @Autowired
  private PlatformTransactionManager transactionManager;

  public SqlAnywhereBatchConfigurer() {
      super();
  }

  public SqlAnywhereBatchConfigurer(DataSource dataSource) {
      super(dataSource);
  }

  @Override
  protected JobRepository createJobRepository() throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDataSource(dataSource);
    factory.setTransactionManager(transactionManager);
    factory.setDatabaseType("SQLSERVER");
    factory.afterPropertiesSet();
    return factory.getObject();
  }
}
请记住首先使用sqlserver.sql模式设置表

您必须在配置中,在
应用程序中定义数据源。属性
我有:

# database
spring.datasource.url=jdbc:sqlanywhere:Server=<your server name>;port=2638
spring.datasource.username=<your username>
spring.datasource.password=<your password>
spring.datasource.driver-class-name=sybase.jdbc4.sqlanywhere.IDriver
# don't create tables on startup
spring.datasource.initialize=false
spring.batch.initializer.enabled=false
#数据库
url=jdbc:sqlanywhere:Server=;端口=2638
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver类名=sybase.jdbc4.sqlanywhere.IDriver
#不要在启动时创建表
spring.datasource.initialize=false
spring.batch.initializer.enabled=false
最后,这里值得一提的是,对于SQL Anywhere,至少
JdbcCursorItemReader
不起作用,因为设置准备语句的获取方向会导致抛出“不受支持的”
SQL异常。您可以通过扩展
JdbcCursorItemReader
并在自己的类中重写
applyStatementSettings
函数(加上一些setter)来解决这个问题