Skip to content

Commit acb63bf

Browse files
authored
Merge pull request #2813 from gallyamb/cursor-cache-oom
[Test case] Using cursor causes OOM #2812
2 parents 51baa56 + 15d0fad commit acb63bf

File tree

7 files changed

+336
-0
lines changed

7 files changed

+336
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2009-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.cursor_cache_oom;
17+
18+
import java.io.IOException;
19+
import java.io.Reader;
20+
import java.lang.reflect.Field;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import org.apache.ibatis.BaseDataTest;
25+
import org.apache.ibatis.cache.CacheKey;
26+
import org.apache.ibatis.cursor.Cursor;
27+
import org.apache.ibatis.cursor.defaults.DefaultCursor;
28+
import org.apache.ibatis.executor.resultset.DefaultResultSetHandler;
29+
import org.apache.ibatis.io.Resources;
30+
import org.apache.ibatis.session.SqlSession;
31+
import org.apache.ibatis.session.SqlSessionFactory;
32+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
33+
import org.apache.ibatis.session.defaults.DefaultSqlSession;
34+
import org.junit.jupiter.api.Assertions;
35+
import org.junit.jupiter.api.BeforeAll;
36+
import org.junit.jupiter.api.Test;
37+
38+
@SuppressWarnings("ALL")
39+
class CursorOomTest {
40+
41+
private static SqlSessionFactory sqlSessionFactory;
42+
43+
@BeforeAll
44+
static void setUp() throws Exception {
45+
// create an SqlSessionFactory
46+
try (Reader reader = Resources.getResourceAsReader(
47+
"org/apache/ibatis/submitted/cursor_cache_oom/mybatis-config.xml")) {
48+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
49+
}
50+
51+
// populate in-memory database
52+
BaseDataTest.runScript(
53+
sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
54+
"org/apache/ibatis/submitted/cursor_cache_oom/CreateDB.sql"
55+
);
56+
}
57+
58+
private static Map<CacheKey, Object> getNestedResultObjects(Cursor<User> users) throws IllegalAccessException,
59+
NoSuchFieldException {
60+
DefaultCursor<User> defaultCursor = (DefaultCursor<User>) users;
61+
Field resultSetHandlerField = DefaultCursor.class.getDeclaredField("resultSetHandler");
62+
resultSetHandlerField.setAccessible(true);
63+
DefaultResultSetHandler defaultResultSetHandler =
64+
(DefaultResultSetHandler) resultSetHandlerField
65+
.get(defaultCursor);
66+
Field nestedResultObjectsField = DefaultResultSetHandler.class.getDeclaredField("nestedResultObjects");
67+
nestedResultObjectsField.setAccessible(true);
68+
return (Map<CacheKey, Object>) nestedResultObjectsField
69+
.get(defaultResultSetHandler);
70+
}
71+
72+
private static List<Cursor<?>> getCursors(SqlSession sqlSession)
73+
throws NoSuchFieldException, IllegalAccessException {
74+
DefaultSqlSession session = (DefaultSqlSession) sqlSession;
75+
Field cursorListField = DefaultSqlSession.class.getDeclaredField("cursorList");
76+
cursorListField.setAccessible(true);
77+
List<Cursor<?>> cursorList =
78+
(List<Cursor<?>>) cursorListField.get(session);
79+
return cursorList;
80+
}
81+
82+
@Test
83+
void shouldNotCacheAllDataForWholeSessionWhileUsingCursor() throws IOException, NoSuchFieldException,
84+
IllegalAccessException {
85+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
86+
Mapper mapper = sqlSession.getMapper(Mapper.class);
87+
try (Cursor<User> users = mapper.fetchUsers()) {
88+
for (User user : users) {
89+
consumeUser(user);
90+
}
91+
Map nestedResultObjects = getNestedResultObjects(users);
92+
93+
Assertions.assertFalse(nestedResultObjects.isEmpty());
94+
95+
// does not pass now
96+
// will be great, if cursor will use constant memory instead of linear one
97+
// Assertions.assertTrue(nestedResultObjects.size() <= 2);
98+
}
99+
100+
List<Cursor<?>> cursorList = getCursors(sqlSession);
101+
102+
// expect that either reference to the cursor itselfis gone or cursor does not contains all the fetched data
103+
// the most preferrable way will be not to cache data, when the row is already processed (see commented
104+
// line above)
105+
Assertions.assertTrue(
106+
cursorList.isEmpty() || getNestedResultObjects((Cursor<User>) cursorList.get(0)).size() <= 2
107+
);
108+
}
109+
}
110+
111+
private void consumeUser(User user) {
112+
// do nothing
113+
}
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2009-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.cursor_cache_oom;
17+
18+
public class Friend {
19+
20+
private String name;
21+
22+
public String getName() {
23+
return name;
24+
}
25+
26+
public void setName(String name) {
27+
this.name = name;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2009-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.cursor_cache_oom;
17+
18+
import org.apache.ibatis.cursor.Cursor;
19+
20+
public interface Mapper {
21+
22+
User getUser(Integer id);
23+
24+
void insertUser(User user);
25+
26+
Cursor<User> fetchUsers();
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2009-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.cursor_cache_oom;
17+
18+
public class User {
19+
20+
private Integer id;
21+
private String name;
22+
private Friend friend;
23+
24+
public Integer getId() {
25+
return id;
26+
}
27+
28+
public void setId(Integer id) {
29+
this.id = id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public Friend getFriend() {
41+
return friend;
42+
}
43+
44+
public void setFriend(Friend friend) {
45+
this.friend = friend;
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--
2+
-- Copyright 2009-2022 the original author or authors.
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- https://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
17+
drop table users if exists;
18+
19+
create table users (
20+
id int,
21+
name varchar(20),
22+
friend_id int
23+
);
24+
25+
create sequence IF NOT EXISTS cursor_cache_oom START WITH 1 INCREMENT BY 1;
26+
27+
insert into users (id, name, friend_id)
28+
SELECT nr, 'test' + nr, mod((nr + 1), 10000) + 1
29+
from unnest(sequence_array(1, 10000, 1)) as i(nr);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2022 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE mapper
20+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
21+
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
22+
23+
<mapper namespace="org.apache.ibatis.submitted.cursor_cache_oom.Mapper">
24+
25+
<select id="getUser" resultType="org.apache.ibatis.submitted.cursor_cache_oom.User">
26+
select * from users where id = #{id}
27+
</select>
28+
29+
<insert id="insertUser">
30+
insert into users values(#{id}, #{name})
31+
</insert>
32+
33+
<select id="fetchUsers" resultMap="User" resultOrdered="true">
34+
select u.id as id, u.name as name, f.name as friend_name
35+
from users as u
36+
left join users as f on u.friend_id = f.id
37+
</select>
38+
39+
<resultMap id="User" type="org.apache.ibatis.submitted.cursor_cache_oom.User">
40+
<id property="id" column="id"/>
41+
<result property="name" column="name"/>
42+
<association property="friend" resultMap="Friend"/>
43+
</resultMap>
44+
45+
<resultMap id="Friend" type="org.apache.ibatis.submitted.cursor_cache_oom.Friend">
46+
<result property="name" column="friend_name"/>
47+
</resultMap>
48+
</mapper>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2022 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE configuration
20+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
21+
"https://mybatis.org/dtd/mybatis-3-config.dtd">
22+
23+
<configuration>
24+
25+
<environments default="development">
26+
<environment id="development">
27+
<transactionManager type="JDBC">
28+
<property name="" value="" />
29+
</transactionManager>
30+
<dataSource type="UNPOOLED">
31+
<property name="driver" value="org.hsqldb.jdbcDriver" />
32+
<property name="url" value="jdbc:hsqldb:mem:basetest" />
33+
<property name="username" value="sa" />
34+
</dataSource>
35+
</environment>
36+
</environments>
37+
38+
<mappers>
39+
<mapper class="org.apache.ibatis.submitted.cursor_cache_oom.Mapper" />
40+
</mappers>
41+
42+
</configuration>

0 commit comments

Comments
 (0)