i attempting create spring batch application. use sql anywhere database, sqlserver
, known database type. make things easier using @springbootapplication
on main class , @enablebatchprocessing
on configuration class.
the problem database driver, sybase.jdbc4.sqlanywhere.idriver
, returns product name "sql anywhere" , not recognized spring, causing various errors. able past of them explicitly creating jobrepositoryfactorybean in configuration class:
/** * can't rely on spring boot can't set database type properly. * * explicitly requiring arguments in constructor, force autowiring * 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"); // 1 instance @ time return jf; }
however, defaultbatchconfigurer
fails in intialize function, because explicitly constructs own jobexplorerfactorybean.
i wonder if there simple way around this, or if have duplicate work in defaultbatchconfigurer
class , define beans myself , remove @enablebatchprocessing
annotation.
i able solve this, , hope helps trying use spring batch database out-of-the-box. necessary extend defaultbatchconfigurer
, override createjobrepository()
function. also, should disable automatic job table creation.
here class created can used base 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 use schema-sqlserver.sql
set tables first.
you must define datasource in configuration, in application.properties
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, worth mentioning here sql anywhere @ least jdbccursoritemreader
not work, because setting fetch direction of prepared statement causes "not supported" sql exception
thrown. can around extending jdbccursoritemreader
, overriding applystatementsettings
function (plus few setters) in own class.
Comments
Post a Comment