Skip to content

Commit

Permalink
DefaultCursor could invoke method of a closed ResultSet on DB2.
Browse files Browse the repository at this point in the history
Should fix mybatis#1345 .
  • Loading branch information
harawata committed Sep 16, 2018
1 parent 7efa720 commit 83df517
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,12 @@ protected T fetchNextObjectFromDatabase() {
return null;
}

final boolean resultSetClosed;
try {
status = CursorStatus.OPEN;
resultSetHandler.handleRowValues(rsw, resultMap, objectWrapperResultHandler, RowBounds.DEFAULT, null);
// ResultSet might have been closed by the driver (e.g. DB2).
resultSetClosed = rsw.getResultSet().isClosed();
} catch (SQLException e) {
throw new RuntimeException(e);
}
Expand All @@ -151,7 +154,7 @@ protected T fetchNextObjectFromDatabase() {
indexWithRowBound++;
}
// No more object or limit reached
if (next == null || getReadItemsCount() == rowBounds.getOffset() + rowBounds.getLimit()) {
if (resultSetClosed || next == null || getReadItemsCount() == rowBounds.getOffset() + rowBounds.getLimit()) {
close();
status = CursorStatus.CONSUMED;
}
Expand Down
200 changes: 200 additions & 0 deletions src/test/java/org/apache/ibatis/cursor/defaults/DefaultCursorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/**
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ibatis.cursor.defaults;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.resultset.DefaultResultSetHandler;
import org.apache.ibatis.executor.resultset.ResultSetWrapper;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class DefaultCursorTest {
@Spy
private ImpatientResultSet rs;
@Mock
protected ResultSetMetaData rsmd;
@Mock
private Connection conn;
@Mock
private DatabaseMetaData dbmd;
@Mock
private Statement stmt;

@SuppressWarnings("unchecked")
@Test
public void shouldCloseImmediatelyIfResultSetIsClosed() throws Exception {
final MappedStatement ms = getNestedAndOrderedMappedStatement();
final ResultMap rm = ms.getResultMaps().get(0);

final Executor executor = null;
final ParameterHandler parameterHandler = null;
final ResultHandler<?> resultHandler = null;
final BoundSql boundSql = null;
final RowBounds rowBounds = RowBounds.DEFAULT;

final DefaultResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, ms, parameterHandler,
resultHandler, boundSql, rowBounds);

when(stmt.getResultSet()).thenReturn(rs);
when(rsmd.getColumnCount()).thenReturn(2);
when(rsmd.getColumnLabel(1)).thenReturn("id");
when(rsmd.getColumnType(1)).thenReturn(Types.INTEGER);
when(rsmd.getColumnClassName(1)).thenReturn(Integer.class.getCanonicalName());
when(rsmd.getColumnLabel(2)).thenReturn("role");
when(rsmd.getColumnType(2)).thenReturn(Types.VARCHAR);
when(rsmd.getColumnClassName(2)).thenReturn(String.class.getCanonicalName());
when(stmt.getConnection()).thenReturn(conn);
when(conn.getMetaData()).thenReturn(dbmd);
when(dbmd.supportsMultipleResultSets()).thenReturn(false);

final ResultSetWrapper rsw = new ResultSetWrapper(rs, ms.getConfiguration());

try (DefaultCursor<?> cursor = new DefaultCursor<>(resultSetHandler, rm, rsw, RowBounds.DEFAULT)) {
Iterator<?> iter = cursor.iterator();
assertTrue(iter.hasNext());
Map<String, Object> map = (Map<String, Object>) iter.next();
assertEquals(Integer.valueOf(1), map.get("id"));
assertEquals("CEO", ((Map<String, Object>) map.get("roles")).get("role"));
assertTrue(cursor.isConsumed());
assertFalse(cursor.isOpen());
assertFalse(iter.hasNext());
}
}

@SuppressWarnings("serial")
private MappedStatement getNestedAndOrderedMappedStatement() {
final Configuration config = new Configuration();
final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();

ResultMap nestedResultMap = new ResultMap.Builder(config, "roleMap", HashMap.class,
new ArrayList<ResultMapping>() {
{
add(new ResultMapping.Builder(config, "role", "role", registry.getTypeHandler(String.class))
.build());
}
}).build();
config.addResultMap(nestedResultMap);

return new MappedStatement.Builder(config, "selectPerson", new StaticSqlSource(config, "select person..."),
SqlCommandType.SELECT).resultMaps(
new ArrayList<ResultMap>() {
{
add(new ResultMap.Builder(config, "personMap", HashMap.class, new ArrayList<ResultMapping>() {
{
add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(Integer.class))
.build());
add(new ResultMapping.Builder(config, "roles").nestedResultMapId("roleMap").build());
}
}).build());
}
})
.resultOrdered(true)
.build();
}

/*
* Simulate a driver that closes ResultSet automatically when next() returns false (e.g. DB2).
*/
protected abstract class ImpatientResultSet implements ResultSet {
private int rowIndex = -1;
private List<Map<String, Object>> rows = new ArrayList<>();

protected ImpatientResultSet() {
Map<String, Object> row = new HashMap<>();
row.put("id", Integer.valueOf(1));
row.put("role", "CEO");
rows.add(row);
}

@Override
public boolean next() throws SQLException {
throwIfClosed();
return ++rowIndex < rows.size();
}

@Override
public boolean isClosed() throws SQLException {
return rowIndex >= rows.size();
}

@Override
public String getString(String columnLabel) throws SQLException {
throwIfClosed();
return (String) rows.get(rowIndex).get(columnLabel);
}

@Override
public int getInt(String columnLabel) throws SQLException {
throwIfClosed();
return (Integer) rows.get(rowIndex).get(columnLabel);
}

@Override
public boolean wasNull() throws SQLException {
throwIfClosed();
return false;
}

@Override
public ResultSetMetaData getMetaData() throws SQLException {
return rsmd;
}

@Override
public int getType() throws SQLException {
throwIfClosed();
return ResultSet.TYPE_FORWARD_ONLY;
}

private void throwIfClosed() throws SQLException {
if (rowIndex >= rows.size()) {
throw new SQLException("Invalid operation: result set is closed.");
}
}
}
}

0 comments on commit 83df517

Please sign in to comment.