forked from mybatis/mybatis-3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes mybatis#1351 Allow ReuseExecutor to reuse statement.
- Closing Statement may be an executor's responsibility. - Using Cursor now requires drivers that support Statement#closeOnCompletion() (JDBC 4.1+).
- Loading branch information
Showing
4 changed files
with
98 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/test/java/org/apache/ibatis/submitted/cursor_simple/PostgresCursorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |