Skip to content

Commit 4391ad9

Browse files
committed
DefaultCursor could invoke method of a closed ResultSet on DB2.
Should fix #1345 .
1 parent c43c9d9 commit 4391ad9

File tree

2 files changed

+204
-1
lines changed

2 files changed

+204
-1
lines changed

src/main/java/org/apache/ibatis/cursor/defaults/DefaultCursor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,12 @@ protected T fetchNextObjectFromDatabase() {
139139
return null;
140140
}
141141

142+
final boolean resultSetClosed;
142143
try {
143144
status = CursorStatus.OPEN;
144145
resultSetHandler.handleRowValues(rsw, resultMap, objectWrapperResultHandler, RowBounds.DEFAULT, null);
146+
// ResultSet might have been closed by the driver (e.g. DB2).
147+
resultSetClosed = rsw.getResultSet().isClosed();
145148
} catch (SQLException e) {
146149
throw new RuntimeException(e);
147150
}
@@ -151,7 +154,7 @@ protected T fetchNextObjectFromDatabase() {
151154
indexWithRowBound++;
152155
}
153156
// No more object or limit reached
154-
if (next == null || getReadItemsCount() == rowBounds.getOffset() + rowBounds.getLimit()) {
157+
if (resultSetClosed || next == null || getReadItemsCount() == rowBounds.getOffset() + rowBounds.getLimit()) {
155158
close();
156159
status = CursorStatus.CONSUMED;
157160
}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/**
2+
* Copyright 2009-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.apache.ibatis.cursor.defaults;
18+
19+
import static org.junit.Assert.*;
20+
import static org.mockito.Mockito.*;
21+
22+
import java.sql.Connection;
23+
import java.sql.DatabaseMetaData;
24+
import java.sql.ResultSet;
25+
import java.sql.ResultSetMetaData;
26+
import java.sql.SQLException;
27+
import java.sql.Statement;
28+
import java.sql.Types;
29+
import java.util.ArrayList;
30+
import java.util.HashMap;
31+
import java.util.Iterator;
32+
import java.util.List;
33+
import java.util.Map;
34+
35+
import org.apache.ibatis.builder.StaticSqlSource;
36+
import org.apache.ibatis.executor.Executor;
37+
import org.apache.ibatis.executor.parameter.ParameterHandler;
38+
import org.apache.ibatis.executor.resultset.DefaultResultSetHandler;
39+
import org.apache.ibatis.executor.resultset.ResultSetWrapper;
40+
import org.apache.ibatis.mapping.BoundSql;
41+
import org.apache.ibatis.mapping.MappedStatement;
42+
import org.apache.ibatis.mapping.ResultMap;
43+
import org.apache.ibatis.mapping.ResultMapping;
44+
import org.apache.ibatis.mapping.SqlCommandType;
45+
import org.apache.ibatis.session.Configuration;
46+
import org.apache.ibatis.session.ResultHandler;
47+
import org.apache.ibatis.session.RowBounds;
48+
import org.apache.ibatis.type.TypeHandlerRegistry;
49+
import org.junit.Test;
50+
import org.junit.runner.RunWith;
51+
import org.mockito.Mock;
52+
import org.mockito.Spy;
53+
import org.mockito.junit.MockitoJUnitRunner;
54+
55+
@RunWith(MockitoJUnitRunner.class)
56+
public class DefaultCursorTest {
57+
@Spy
58+
private ImpatientResultSet rs;
59+
@Mock
60+
protected ResultSetMetaData rsmd;
61+
@Mock
62+
private Connection conn;
63+
@Mock
64+
private DatabaseMetaData dbmd;
65+
@Mock
66+
private Statement stmt;
67+
68+
@SuppressWarnings("unchecked")
69+
@Test
70+
public void shouldCloseImmediatelyIfResultSetIsClosed() throws Exception {
71+
final MappedStatement ms = getNestedAndOrderedMappedStatement();
72+
final ResultMap rm = ms.getResultMaps().get(0);
73+
74+
final Executor executor = null;
75+
final ParameterHandler parameterHandler = null;
76+
final ResultHandler<?> resultHandler = null;
77+
final BoundSql boundSql = null;
78+
final RowBounds rowBounds = RowBounds.DEFAULT;
79+
80+
final DefaultResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, ms, parameterHandler,
81+
resultHandler, boundSql, rowBounds);
82+
83+
when(stmt.getResultSet()).thenReturn(rs);
84+
when(rsmd.getColumnCount()).thenReturn(2);
85+
when(rsmd.getColumnLabel(1)).thenReturn("id");
86+
when(rsmd.getColumnType(1)).thenReturn(Types.INTEGER);
87+
when(rsmd.getColumnClassName(1)).thenReturn(Integer.class.getCanonicalName());
88+
when(rsmd.getColumnLabel(2)).thenReturn("role");
89+
when(rsmd.getColumnType(2)).thenReturn(Types.VARCHAR);
90+
when(rsmd.getColumnClassName(2)).thenReturn(String.class.getCanonicalName());
91+
when(stmt.getConnection()).thenReturn(conn);
92+
when(conn.getMetaData()).thenReturn(dbmd);
93+
when(dbmd.supportsMultipleResultSets()).thenReturn(false);
94+
95+
final ResultSetWrapper rsw = new ResultSetWrapper(rs, ms.getConfiguration());
96+
97+
try (DefaultCursor<?> cursor = new DefaultCursor<>(resultSetHandler, rm, rsw, RowBounds.DEFAULT)) {
98+
Iterator<?> iter = cursor.iterator();
99+
assertTrue(iter.hasNext());
100+
Map<String, Object> map = (Map<String, Object>) iter.next();
101+
assertEquals(Integer.valueOf(1), map.get("id"));
102+
assertEquals("CEO", ((Map<String, Object>) map.get("roles")).get("role"));
103+
assertTrue(cursor.isConsumed());
104+
assertFalse(cursor.isOpen());
105+
assertFalse(iter.hasNext());
106+
}
107+
}
108+
109+
@SuppressWarnings("serial")
110+
private MappedStatement getNestedAndOrderedMappedStatement() {
111+
final Configuration config = new Configuration();
112+
final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
113+
114+
ResultMap nestedResultMap = new ResultMap.Builder(config, "roleMap", HashMap.class,
115+
new ArrayList<ResultMapping>() {
116+
{
117+
add(new ResultMapping.Builder(config, "role", "role", registry.getTypeHandler(String.class))
118+
.build());
119+
}
120+
}).build();
121+
config.addResultMap(nestedResultMap);
122+
123+
return new MappedStatement.Builder(config, "selectPerson", new StaticSqlSource(config, "select person..."),
124+
SqlCommandType.SELECT).resultMaps(
125+
new ArrayList<ResultMap>() {
126+
{
127+
add(new ResultMap.Builder(config, "personMap", HashMap.class, new ArrayList<ResultMapping>() {
128+
{
129+
add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(Integer.class))
130+
.build());
131+
add(new ResultMapping.Builder(config, "roles").nestedResultMapId("roleMap").build());
132+
}
133+
}).build());
134+
}
135+
})
136+
.resultOrdered(true)
137+
.build();
138+
}
139+
140+
/*
141+
* Simulate a driver that closes ResultSet automatically when next() returns false (e.g. DB2).
142+
*/
143+
protected abstract class ImpatientResultSet implements ResultSet {
144+
private int rowIndex = -1;
145+
private List<Map<String, Object>> rows = new ArrayList<>();
146+
147+
protected ImpatientResultSet() {
148+
Map<String, Object> row = new HashMap<>();
149+
row.put("id", Integer.valueOf(1));
150+
row.put("role", "CEO");
151+
rows.add(row);
152+
}
153+
154+
@Override
155+
public boolean next() throws SQLException {
156+
throwIfClosed();
157+
return ++rowIndex < rows.size();
158+
}
159+
160+
@Override
161+
public boolean isClosed() throws SQLException {
162+
return rowIndex >= rows.size();
163+
}
164+
165+
@Override
166+
public String getString(String columnLabel) throws SQLException {
167+
throwIfClosed();
168+
return (String) rows.get(rowIndex).get(columnLabel);
169+
}
170+
171+
@Override
172+
public int getInt(String columnLabel) throws SQLException {
173+
throwIfClosed();
174+
return (Integer) rows.get(rowIndex).get(columnLabel);
175+
}
176+
177+
@Override
178+
public boolean wasNull() throws SQLException {
179+
throwIfClosed();
180+
return false;
181+
}
182+
183+
@Override
184+
public ResultSetMetaData getMetaData() throws SQLException {
185+
return rsmd;
186+
}
187+
188+
@Override
189+
public int getType() throws SQLException {
190+
throwIfClosed();
191+
return ResultSet.TYPE_FORWARD_ONLY;
192+
}
193+
194+
private void throwIfClosed() throws SQLException {
195+
if (rowIndex >= rows.size()) {
196+
throw new SQLException("Invalid operation: result set is closed.");
197+
}
198+
}
199+
}
200+
}

0 commit comments

Comments
 (0)