问题描述
我想用 Spring boot 实现 Spring Batch,因为我们使用的是 informix 数据库,当我的 spring boot 应用程序启动时,我遇到了以下异常.
I want to implement Spring Batch with Spring boot , since we are using informix database I'm running into the following exception when my spring boot App starts up.
配置:
@Bean
public DataSource dataSource() throws SQLException {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
dataSource.setUrl(dataSourceProperties.getDbUrl());
dataSource.setUsername(dataSourceProperties.getDbUsername());
dataSource.setPassword(dataSourceProperties.getDbPassword())
return dataSource;
}
@Bean
public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource){
DataSourceTransactionManager manager=new DataSourceTransactionManager(dataSource);
return manager;
}
@Bean
public JobRepositoryFactoryBean jobRepositoryFactoryBean( DataSourceTransactionManager txManager,DataSource dataSource ) throws Exception{
JobRepositoryFactoryBean job=new JobRepositoryFactoryBean();
job.setDataSource(dataSource);
job.setDatabaseType("oracle");
job.setTransactionManager(txManager);
job.afterPropertiesSet();
return job;
}
@Bean
public ItemReader<Person> reader() {
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
reader.setResource(new ClassPathResource("sample-data.csv"));
reader.setLineMapper(new DefaultLineMapper<Person>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[] { "firstName", "lastName" });
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
setTargetType(Person.class);
}});
}});
return reader;
}
@Bean
public ItemProcessor<Person, Person> processor() {
return new PersonItemProcessor();
}
@Bean
public ItemWriter<Person> writer(DataSource dataSource) {
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
writer.setDataSource(dataSource);
writer.afterPropertiesSet();
return writer;
}
@Bean
public Job importUserJob(JobBuilderFactory jobs, Step s1, JobExecutionListener listener,JobRepositoryFactoryBean jobRepositoryFactoryBean) throws Exception {
return jobs.get("importUserJob")
.repository(jobRepositoryFactoryBean.getObject())
.incrementer(new RunIdIncrementer())
.listener(listener)
.flow(s1)
.end()
.build();
}
@Bean
public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Person> reader,
ItemWriter<Person> writer, ItemProcessor<Person, Person> processor) {
return stepBuilderFactory.get("step1")
.<Person, Person> chunk(10)
.reader(reader)
.processor(processor)
.writer(writer)
.build();
}
异常
由:java.lang.IllegalArgumentException:未找到产品名称的 DatabaseType:[Informix 动态服务器]在 org.springframework.batch.support.DatabaseType.fromProductName(DatabaseType.java:79)在 org.springframework.batch.support.DatabaseType.fromMetaData(DatabaseType.java:110)在 org.springframework.boot.autoconfigure.batch.BatchDatabaseInitializer.getDatabaseType(BatchDatabaseInitializer.java:71)在 org.springframework.boot.autoconfigure.batch.BatchDatabaseInitializer.initialize(BatchDatabaseInitializer.java:50)在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在 java.lang.reflect.Method.invoke(Method.java:497)在 org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:349)在 org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:300)在 org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
caused by: java.lang.IllegalArgumentException: DatabaseType not found for product name: [Informix Dynamic Server] at org.springframework.batch.support.DatabaseType.fromProductName(DatabaseType.java:79) at org.springframework.batch.support.DatabaseType.fromMetaData(DatabaseType.java:110) at org.springframework.boot.autoconfigure.batch.BatchDatabaseInitializer.getDatabaseType(BatchDatabaseInitializer.java:71) at org.springframework.boot.autoconfigure.batch.BatchDatabaseInitializer.initialize(BatchDatabaseInitializer.java:50) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:349) at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:300) at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
推荐答案
Spring Batch 似乎不直接支持 Informix.
Spring Batch doesn't seem to directly support Informix.
来自 org.springframework.batch.support.DatabaseType
:
DERBY("Apache Derby"),
DB2("DB2"),
DB2ZOS("DB2ZOS"),
HSQL("HSQL Database Engine"),
SQLSERVER("Microsoft SQL Server"),
MYSQL("MySQL"),
ORACLE("Oracle"),
POSTGRES("PostgreSQL"),
SYBASE("Sybase"), H2("H2");
但是,正如这里所说:http://forum.spring.io/forum/spring-projects/batch/61097-why-spring-batch-doesn-t-support-informix-database,看来你可以将您的数据库类型设置为 Oracle,它将支持 Informix.
But, as was said here : http://forum.spring.io/forum/spring-projects/batch/61097-why-spring-batch-doesn-t-support-informix-database, it seems you can set your database type to Oracle and it will support Informix.
这篇关于Spring 批处理 --- 未找到产品名称的 DatabaseType:[Informix 动态服务器]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!