Skip to content

Commit 9e786dc

Browse files
committed
selectCursor() should respect affectData as well
1 parent 76237b0 commit 9e786dc

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSession.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public <T> Cursor<T> selectCursor(String statement, Object parameter) {
120120
public <T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds) {
121121
try {
122122
MappedStatement ms = configuration.getMappedStatement(statement);
123+
dirty |= ms.isDirtySelect();
123124
Cursor<T> cursor = executor.queryCursor(ms, wrapCollection(parameter), rowBounds);
124125
registerCursor(cursor);
125126
return cursor;

src/test/java/org/apache/ibatis/submitted/dirty_select/DirtySelectTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

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

20+
import java.util.Iterator;
21+
2022
import org.apache.ibatis.BaseDataTest;
23+
import org.apache.ibatis.cursor.Cursor;
2124
import org.apache.ibatis.mapping.Environment;
2225
import org.apache.ibatis.session.Configuration;
2326
import org.apache.ibatis.session.SqlSession;
@@ -101,6 +104,27 @@ void shouldRollbackIfCalled_Provider() {
101104
}
102105
}
103106

107+
@Test
108+
void shouldRollbackIfCalled_Cursor() throws Exception {
109+
Integer id;
110+
try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) {
111+
Mapper mapper = sqlSession.getMapper(Mapper.class);
112+
try (Cursor<User> cursor = mapper.insertReturnCursor("Kate")) {
113+
Iterator<User> iterator = cursor.iterator();
114+
User user = iterator.next();
115+
id = user.getId();
116+
assertNotNull(id);
117+
assertEquals("Kate", user.getName());
118+
}
119+
sqlSession.rollback();
120+
}
121+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
122+
Mapper mapper = sqlSession.getMapper(Mapper.class);
123+
User user = mapper.selectById(id);
124+
assertNull(user);
125+
}
126+
}
127+
104128
@Test
105129
void shouldNonDirtySelectNotUnsetDirtyFlag() {
106130
Integer id;
@@ -124,4 +148,30 @@ void shouldNonDirtySelectNotUnsetDirtyFlag() {
124148
}
125149
}
126150

151+
@Test
152+
void shouldNonDirtySelectNotUnsetDirtyFlag_Cursor() throws Exception {
153+
Integer id;
154+
try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) {
155+
Mapper mapper = sqlSession.getMapper(Mapper.class);
156+
// INSERT
157+
User user = new User();
158+
user.setName("Bobby");
159+
mapper.insert(user);
160+
id = user.getId();
161+
assertNotNull(id);
162+
assertEquals("Bobby", user.getName());
163+
// Non-dirty SELECT
164+
try (Cursor<User> cursor = mapper.selectCursorById(id)) {
165+
Iterator<User> iterator = cursor.iterator();
166+
iterator.next();
167+
}
168+
sqlSession.rollback();
169+
}
170+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
171+
Mapper mapper = sqlSession.getMapper(Mapper.class);
172+
User user = mapper.selectById(id);
173+
assertNull(user);
174+
}
175+
}
176+
127177
}

src/test/java/org/apache/ibatis/submitted/dirty_select/Mapper.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,22 @@
1919
import org.apache.ibatis.annotations.Options;
2020
import org.apache.ibatis.annotations.Select;
2121
import org.apache.ibatis.annotations.SelectProvider;
22+
import org.apache.ibatis.cursor.Cursor;
2223

2324
public interface Mapper {
2425

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

29+
@Select("select * from users where id = #{id}")
30+
Cursor<User> selectCursorById(Integer id);
31+
2832
@Select(value = "insert into users (name) values (#{name}) returning id, name", affectData = true)
2933
User insertReturn(String name);
3034

35+
@Select(value = "insert into users (name) values (#{name}) returning id, name", affectData = true)
36+
Cursor<User> insertReturnCursor(String name);
37+
3138
User insertReturnXml(String name);
3239

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

0 commit comments

Comments
 (0)