Skip to content

Commit

Permalink
selectCursor() should respect affectData as well
Browse files Browse the repository at this point in the history
  • Loading branch information
harawata committed Nov 27, 2022
1 parent 76237b0 commit 9e786dc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public <T> Cursor<T> selectCursor(String statement, Object parameter) {
public <T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds) {
try {
MappedStatement ms = configuration.getMappedStatement(statement);
dirty |= ms.isDirtySelect();
Cursor<T> cursor = executor.queryCursor(ms, wrapCollection(parameter), rowBounds);
registerCursor(cursor);
return cursor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

import static org.junit.jupiter.api.Assertions.*;

import java.util.Iterator;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
Expand Down Expand Up @@ -101,6 +104,27 @@ void shouldRollbackIfCalled_Provider() {
}
}

@Test
void shouldRollbackIfCalled_Cursor() throws Exception {
Integer id;
try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
try (Cursor<User> cursor = mapper.insertReturnCursor("Kate")) {
Iterator<User> iterator = cursor.iterator();
User user = iterator.next();
id = user.getId();
assertNotNull(id);
assertEquals("Kate", user.getName());
}
sqlSession.rollback();
}
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
User user = mapper.selectById(id);
assertNull(user);
}
}

@Test
void shouldNonDirtySelectNotUnsetDirtyFlag() {
Integer id;
Expand All @@ -124,4 +148,30 @@ void shouldNonDirtySelectNotUnsetDirtyFlag() {
}
}

@Test
void shouldNonDirtySelectNotUnsetDirtyFlag_Cursor() throws Exception {
Integer id;
try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
// INSERT
User user = new User();
user.setName("Bobby");
mapper.insert(user);
id = user.getId();
assertNotNull(id);
assertEquals("Bobby", user.getName());
// Non-dirty SELECT
try (Cursor<User> cursor = mapper.selectCursorById(id)) {
Iterator<User> iterator = cursor.iterator();
iterator.next();
}
sqlSession.rollback();
}
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
User user = mapper.selectById(id);
assertNull(user);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,22 @@
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.cursor.Cursor;

public interface Mapper {

@Select("select * from users where id = #{id}")
User selectById(Integer id);

@Select("select * from users where id = #{id}")
Cursor<User> selectCursorById(Integer id);

@Select(value = "insert into users (name) values (#{name}) returning id, name", affectData = true)
User insertReturn(String name);

@Select(value = "insert into users (name) values (#{name}) returning id, name", affectData = true)
Cursor<User> insertReturnCursor(String name);

User insertReturnXml(String name);

@SelectProvider(type = MyProvider.class, method = "getSql", affectData = true)
Expand Down

0 comments on commit 9e786dc

Please sign in to comment.