File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed
main/java/org/apache/ibatis/cursor/defaults
test/java/org/apache/ibatis/submitted/cursor_simple Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -97,6 +97,9 @@ public Iterator<T> iterator() {
97
97
if (iteratorRetrieved ) {
98
98
throw new IllegalStateException ("Cannot open more than one iterator on a Cursor" );
99
99
}
100
+ if (isClosed ()) {
101
+ throw new IllegalStateException ("A Cursor is already closed." );
102
+ }
100
103
iteratorRetrieved = true ;
101
104
return cursorIterator ;
102
105
}
Original file line number Diff line number Diff line change @@ -361,4 +361,31 @@ public void shouldGetAllUserUsingAnnotationBasedMapper() {
361
361
}
362
362
}
363
363
364
+ @ Test
365
+ public void shouldThrowIllegalStateExceptionUsingIteratorOnSessionClosed () {
366
+ Cursor <User > usersCursor ;
367
+ try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
368
+ usersCursor = sqlSession .getMapper (Mapper .class ).getAllUsers ();
369
+ }
370
+ try {
371
+ usersCursor .iterator ();
372
+ Assert .fail ("Should throws the IllegalStateException when call the iterator method after session is closed." );
373
+ } catch (IllegalStateException e ) {
374
+ Assert .assertEquals ("A Cursor is already closed." , e .getMessage ());
375
+ }
376
+
377
+ // verify for checking order
378
+ try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
379
+ usersCursor = sqlSession .getMapper (Mapper .class ).getAllUsers ();
380
+ usersCursor .iterator ();
381
+ }
382
+ try {
383
+ usersCursor .iterator ();
384
+ Assert .fail ("Should throws the IllegalStateException when call the iterator already." );
385
+ } catch (IllegalStateException e ) {
386
+ Assert .assertEquals ("Cannot open more than one iterator on a Cursor" , e .getMessage ());
387
+ }
388
+
389
+ }
390
+
364
391
}
You can’t perform that action at this time.
0 commit comments