2017-04-05 35 views
0

我試圖創建一個Spring Batch的應用Spring Batch的。我們使用SQL Anywhere數據庫,該數據庫實際上是SQLSERVER,這是一種已知的數據庫類型。爲了讓事情我使用@SpringBootApplication我的主類和@EnableBatchProcessing更容易在我的配置類。使用帶有自動配置和非標準數據庫

的問題是,我的數據庫驅動程序,sybase.jdbc4.sqlanywhere.IDriver,返回一個產品名稱「SQL Anywhere的」,這是不被認可的春天,導致各種錯誤。我能夠在我的配置類明確創建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未能在intialize功能,因爲它明確地構建自己的JobExplorerFactoryBean。

我想知道是否有一些簡單的解決方法,或者我將不得不復制DefaultBatchConfigurer類中的工作,並自己定義所有bean並刪除@EnableBatchProcessing註釋。

回答

0

我能解決這個問題,我希望它能幫助任何人試圖使用Spring Batch的使用是超出現成的數據庫。有必要延長DefaultBatchConfigurer並重寫createJobRepository()功能。另外,您應該禁用自動作業表創建。

這裏是可以用來作爲任何SQL Anywhere的春天批處理作業基地我創建的類:

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

記住使用schema-sqlserver.sql先設置你的表。

必須在您的配置定義一個數據源,在application.properties我:

# 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 

最後,值得一提的是爲SQL Anywhere至少JdbcCursorItemReader不起作用,因爲設置抓取的方向準備好的聲明導致「不支持」SQL Exception被拋出。您可以通過擴展JdbcCursorItemReader並覆蓋在自己的類的applyStatementSettings功能(加幾制定者)解決這個問題。