spring - JdbcCursorItemReader 是否必须关闭连接?
问题描述
Spring Batch 文档中有一个示例(https://docs.spring.io/spring-batch/docs/4.2.x/reference/html/readersAndWriters.html#cursorBasedItemReaders)
public ItemReader<RevertedBillCredit> verifyCreditReader() {
JdbcCursorItemReader itemReader = new JdbcCursorItemReader();
itemReader.setDataSource(dataSource);
itemReader.setSql("SELECT ID, NAME, CREDIT from CUSTOMER");
itemReader.setRowMapper(new CustomerCreditRowMapper());
int counter = 0;
ExecutionContext executionContext = new ExecutionContext();
itemReader.open(executionContext);
Object customerCredit = new Object();
while(customerCredit != null){
customerCredit = itemReader.read();
counter++;
}
itemReader.close();
}
我发现有些文档说 JdbcCursorItemReader 会在查询后自动关闭连接。但是,为什么我们需要 itemReader.close() 呢?JdbcCursorItemReader 是否必须手动关闭连接?
解决方案
The snippet from reference docs you linked explains how things work behind the scene, but when the reader is used in a chunk-oriented step, you don't need to call open/close
methods yourself, Spring Batch will do it. So your item reader bean definition should be something like:
public ItemReader<RevertedBillCredit> verifyCreditReader() {
JdbcCursorItemReader itemReader = new JdbcCursorItemReader();
itemReader.setDataSource(dataSource);
itemReader.setSql("SELECT ID, NAME, CREDIT from CUSTOMER");
itemReader.setRowMapper(new CustomerCreditRowMapper());
return itemReader;
}
推荐阅读
- java - List.addAll 方法上的 Java ConcurrentModificationException 没有任何循环
- list - Prolog 成员谓词
- c# - 如何使用 C# 按列和行对项目列表进行分组
- jupyter-notebook - 如何更改 Jupyter 的编辑模式快捷方式以在 Mac OSX 上使用修饰符
- c++ - 复制构造函数与 std::initializer_list 构造函数
- java - 不兼容的类型:char[] 无法转换为 CharSequence
- git - 命令 go get -u 命令出错。无法获取包裹
- java - 无法计算正多边形的面积 - 使用切线公式得出错误的结果
- reactjs - 用 typescript 构造一个通用接口
- javascript - 默认情况下,按钮通过 javascript 获取不正确的 PHP 值