Skip to content

Commit

Permalink
fixes mybatis#1351 Allow ReuseExecutor to reuse statement.
Browse files Browse the repository at this point in the history
- Closing Statement may be an executor's responsibility.
- Using Cursor now requires drivers that support Statement#closeOnCompletion() (JDBC 4.1+).
  • Loading branch information
harawata committed Oct 6, 2018
1 parent ed6c653 commit 2e4f495
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.NoSuchElementException;

Expand Down Expand Up @@ -113,12 +112,7 @@ public void close() {
ResultSet rs = rsw.getResultSet();
try {
if (rs != null) {
Statement statement = rs.getStatement();

rs.close();
if (statement != null) {
statement.close();
}
}
status = CursorStatus.CLOSED;
} catch (SQLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ protected <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowB
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, null, boundSql);
Connection connection = getConnection(ms.getStatementLog());
Statement stmt = handler.prepare(connection, transaction.getTimeout());
stmt.closeOnCompletion();
handler.parameterize(stmt);
return handler.<E>queryCursor(stmt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ protected <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowB
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, null, boundSql);
Statement stmt = prepareStatement(handler, ms.getStatementLog());
stmt.closeOnCompletion();
return handler.<E>queryCursor(stmt);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* 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.submitted.cursor_simple;

import static org.junit.Assert.*;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Iterator;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.test.EmbeddedPostgresqlTests;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres;
import ru.yandex.qatools.embed.postgresql.util.SocketUtil;

@Category(EmbeddedPostgresqlTests.class)
public class PostgresCursorTest {

private static final EmbeddedPostgres postgres = new EmbeddedPostgres();

private static SqlSessionFactory sqlSessionFactory;

@BeforeClass
public static void setUp() throws Exception {
// Launch PostgreSQL server. Download / unarchive if necessary.
String url = postgres.start(
EmbeddedPostgres.cachedRuntimeConfig(Paths.get(System.getProperty("java.io.tmpdir"), "pgembed")), "localhost",
SocketUtil.findFreePort(), "cursor_simple", "postgres", "root", Collections.emptyList());

Configuration configuration = new Configuration();
Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource(
"org.postgresql.Driver", url, null));
configuration.setEnvironment(environment);
configuration.addMapper(Mapper.class);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
"org/apache/ibatis/submitted/cursor_simple/CreateDB.sql");
}

@AfterClass
public static void tearDown() {
postgres.stop();
}

@Test
public void shouldBeAbleToReuseStatement() throws IOException {
// #1351
try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.REUSE)) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
{
Cursor<User> usersCursor = mapper.getAllUsers();
Iterator<User> iterator = usersCursor.iterator();
User user = iterator.next();
assertEquals("User1", user.getName());
usersCursor.close();
}
{
Cursor<User> usersCursor = mapper.getAllUsers();
Iterator<User> iterator = usersCursor.iterator();
User user = iterator.next();
assertEquals("User1", user.getName());
usersCursor.close();
}
}
}
}

0 comments on commit 2e4f495

Please sign in to comment.