Using Spring Batch with auto-configure and a non-standard database

后端 未结 1 1952
夕颜
夕颜 2020-12-22 07:16

I am attempting to create a Spring Batch application. We use a SQL Anywhere database, which is effectively SQLSERVER, a known database type. To make things easi

相关标签:
1条回答
  • 2020-12-22 08:12

    I was able to solve this, and I hope it helps anyone trying to use Spring Batch with a database that is out-of-the-box. It is necessary to extend DefaultBatchConfigurer and override the createJobRepository() function. Also, you should disable the automatic job table creation.

    Here is the class I created that can be used as a base for any SQL Anywhere Spring Batch job:

    @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();
      }
    }
    

    Remember to use the schema-sqlserver.sql to set up your tables first.

    You must define a datasource in your configuration, in application.properties I have:

    # 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
    

    Finally, it is worth mentioning here that for SQL Anywhere at least the JdbcCursorItemReader does not work, because setting the fetch direction of the prepared statement causes a "not supported" SQL Exception to be thrown. You can get around this by extending JdbcCursorItemReader and overriding the applyStatementSettings function (plus a few setters) in your own class.

    0 讨论(0)
提交回复
热议问题