[英]How to execute a stored procedure in postgresql with spring batch?
I'm doing batch processing with spring batch. 我正在使用Spring Batch进行批处理。 First, It reads a csv file, after that, I want to update in the database, for that I use a stored procedure to do different action per row.
首先,它读取一个csv文件,此后,我想在数据库中进行更新,因为我使用存储过程对每一行执行不同的操作。 The code is this:
代码是这样的:
Reader:
@Bean
@StepScope
public ItemReader<Comprobante> csvFileReader( @Value("#{jobParameters['filename']}") String filename) throws UnexpectedInputException, ParseException{
FlatFileItemReader<Comprobante> reader = new FlatFileItemReader<>();
reader.setResource(new FileSystemResource(fileDir + filename));
reader.setName("CSV-Reader");
reader.setLinesToSkip(1);
reader.setLineMapper(lineMapper());
reader.open(new ExecutionContext());
return reader;
}
@Bean
public LineMapper<Comprobante> lineMapper() {
DefaultLineMapper<Comprobante> defaultLineMapper = new DefaultLineMapper<>();
DelimitedLineTokenizer delimitedLineTokenizer = new DelimitedLineTokenizer();
delimitedLineTokenizer.setDelimiter(",");
delimitedLineTokenizer.setStrict(false);
delimitedLineTokenizer.setNames(new String[] {"cid", "ruc", "tipo", "serie", "numero","fecha", "monto"});
BeanWrapperFieldSetMapper<Comprobante> beanWrapperFieldSetMapper = new BeanWrapperFieldSetMapper<>();
beanWrapperFieldSetMapper.setTargetType(Comprobante.class);
defaultLineMapper.setLineTokenizer(delimitedLineTokenizer);
defaultLineMapper.setFieldSetMapper(beanWrapperFieldSetMapper);
return defaultLineMapper;
}
Writer:
@Bean
public ItemWriter<Comprobante> csvFileWrite (DataSource dataSource, NamedParameterJdbcTemplate jdbcTemplate){
JdbcBatchItemWriter<Comprobante> writer = new JdbcBatchItemWriter<>();
writer.setDataSource(dataSource);
writer.setJdbcTemplate(jdbcTemplate);
writer.setSql("select validateVoucher(? , ?);"); // This is the code with error.
ItemPreparedStatementSetter<Comprobante> valueSetter = new comprobantePreparedStatementSetter();
writer.setItemPreparedStatementSetter(valueSetter);
return writer;
}
The log shows this message: 日志显示此消息:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [select validateVoucher(? , ?);]; nested exception is java.sql.BatchUpdateException: Batch entry 0 select validateVoucher('40883884' , '0') was aborted: ERROR: function validatevoucher(character varying, character varying) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 8 Call getNextException to see other errors in the batch.
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101) ~[spring-jdbc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) ~[spring-jdbc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1444) ~[spring-jdbc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:632) ~[spring-jdbc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:646) ~[spring-jdbc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
.......
You are going to need to create your own ItemWriter
implementation that handles this call. 您将需要创建自己的
ItemWriter
实现来处理此调用。 There is nothing within the framework out of the box that supports writing to a stored procedure. 开箱即用的框架中没有任何东西支持写入存储过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.