forked from mybatis/mybatis-3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using cursor causes OOM mybatis#2812
- Loading branch information
Showing
7 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
src/test/java/org/apache/ibatis/submitted/cursor_cache_oom/CursorOomTest.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,114 @@ | ||
/* | ||
* Copyright 2009-2023 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 | ||
* | ||
* https://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_cache_oom; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.lang.reflect.Field; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.ibatis.BaseDataTest; | ||
import org.apache.ibatis.cache.CacheKey; | ||
import org.apache.ibatis.cursor.Cursor; | ||
import org.apache.ibatis.cursor.defaults.DefaultCursor; | ||
import org.apache.ibatis.executor.resultset.DefaultResultSetHandler; | ||
import org.apache.ibatis.io.Resources; | ||
import org.apache.ibatis.session.SqlSession; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||
import org.apache.ibatis.session.defaults.DefaultSqlSession; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@SuppressWarnings("ALL") | ||
class CursorOomTest { | ||
|
||
private static SqlSessionFactory sqlSessionFactory; | ||
|
||
@BeforeAll | ||
static void setUp() throws Exception { | ||
// create an SqlSessionFactory | ||
try (Reader reader = Resources.getResourceAsReader( | ||
"org/apache/ibatis/submitted/cursor_cache_oom/mybatis-config.xml")) { | ||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); | ||
} | ||
|
||
// populate in-memory database | ||
BaseDataTest.runScript( | ||
sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), | ||
"org/apache/ibatis/submitted/cursor_cache_oom/CreateDB.sql" | ||
); | ||
} | ||
|
||
private static Map<CacheKey, Object> getNestedResultObjects(Cursor<User> users) throws IllegalAccessException, | ||
NoSuchFieldException { | ||
DefaultCursor<User> defaultCursor = (DefaultCursor<User>) users; | ||
Field resultSetHandlerField = DefaultCursor.class.getDeclaredField("resultSetHandler"); | ||
resultSetHandlerField.setAccessible(true); | ||
DefaultResultSetHandler defaultResultSetHandler = | ||
(DefaultResultSetHandler) resultSetHandlerField | ||
.get(defaultCursor); | ||
Field nestedResultObjectsField = DefaultResultSetHandler.class.getDeclaredField("nestedResultObjects"); | ||
nestedResultObjectsField.setAccessible(true); | ||
return (Map<CacheKey, Object>) nestedResultObjectsField | ||
.get(defaultResultSetHandler); | ||
} | ||
|
||
private static List<Cursor<?>> getCursors(SqlSession sqlSession) | ||
throws NoSuchFieldException, IllegalAccessException { | ||
DefaultSqlSession session = (DefaultSqlSession) sqlSession; | ||
Field cursorListField = DefaultSqlSession.class.getDeclaredField("cursorList"); | ||
cursorListField.setAccessible(true); | ||
List<Cursor<?>> cursorList = | ||
(List<Cursor<?>>) cursorListField.get(session); | ||
return cursorList; | ||
} | ||
|
||
@Test | ||
void shouldNotCacheAllDataForWholeSessionWhileUsingCursor() throws IOException, NoSuchFieldException, | ||
IllegalAccessException { | ||
try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
Mapper mapper = sqlSession.getMapper(Mapper.class); | ||
try (Cursor<User> users = mapper.fetchUsers()) { | ||
for (User user : users) { | ||
consumeUser(user); | ||
} | ||
Map nestedResultObjects = getNestedResultObjects(users); | ||
|
||
Assertions.assertFalse(nestedResultObjects.isEmpty()); | ||
|
||
// does not pass now | ||
// will be great, if cursor will use constant memory instead of linear one | ||
// Assertions.assertTrue(nestedResultObjects.size() <= 2); | ||
} | ||
|
||
List<Cursor<?>> cursorList = getCursors(sqlSession); | ||
|
||
// expect that either reference to the cursor itselfis gone or cursor does not contains all the fetched data | ||
// the most preferrable way will be not to cache data, when the row is already processed (see commented | ||
// line above) | ||
Assertions.assertTrue( | ||
cursorList.isEmpty() || getNestedResultObjects((Cursor<User>) cursorList.get(0)).isEmpty() | ||
); | ||
} | ||
} | ||
|
||
private void consumeUser(User user) { | ||
// do nothing | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/org/apache/ibatis/submitted/cursor_cache_oom/Friend.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,29 @@ | ||
/* | ||
* Copyright 2009-2022 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 | ||
* | ||
* https://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_cache_oom; | ||
|
||
public class Friend { | ||
|
||
private String name; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/test/java/org/apache/ibatis/submitted/cursor_cache_oom/Mapper.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,27 @@ | ||
/* | ||
* Copyright 2009-2022 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 | ||
* | ||
* https://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_cache_oom; | ||
|
||
import org.apache.ibatis.cursor.Cursor; | ||
|
||
public interface Mapper { | ||
|
||
User getUser(Integer id); | ||
|
||
void insertUser(User user); | ||
|
||
Cursor<User> fetchUsers(); | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/java/org/apache/ibatis/submitted/cursor_cache_oom/User.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,47 @@ | ||
/* | ||
* Copyright 2009-2022 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 | ||
* | ||
* https://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_cache_oom; | ||
|
||
public class User { | ||
|
||
private Integer id; | ||
private String name; | ||
private Friend friend; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Friend getFriend() { | ||
return friend; | ||
} | ||
|
||
public void setFriend(Friend friend) { | ||
this.friend = friend; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/resources/org/apache/ibatis/submitted/cursor_cache_oom/CreateDB.sql
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,29 @@ | ||
-- | ||
-- Copyright 2009-2022 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 | ||
-- | ||
-- https://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. | ||
-- | ||
|
||
drop table users if exists; | ||
|
||
create table users ( | ||
id int, | ||
name varchar(20), | ||
friend_id int | ||
); | ||
|
||
create sequence IF NOT EXISTS cursor_cache_oom START WITH 1 INCREMENT BY 1; | ||
|
||
insert into users (id, name, friend_id) | ||
SELECT nr, 'test' + nr, mod((nr + 1), 10000) + 1 | ||
from unnest(sequence_array(1, 10000, 1)) as i(nr); |
48 changes: 48 additions & 0 deletions
48
src/test/resources/org/apache/ibatis/submitted/cursor_cache_oom/Mapper.xml
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,48 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2009-2022 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 | ||
https://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. | ||
--> | ||
<!DOCTYPE mapper | ||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
"https://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
|
||
<mapper namespace="org.apache.ibatis.submitted.cursor_cache_oom.Mapper"> | ||
|
||
<select id="getUser" resultType="org.apache.ibatis.submitted.cursor_cache_oom.User"> | ||
select * from users where id = #{id} | ||
</select> | ||
|
||
<insert id="insertUser"> | ||
insert into users values(#{id}, #{name}) | ||
</insert> | ||
|
||
<select id="fetchUsers" resultMap="User"> | ||
select u.id as id, u.name as name, f.name as friend_name | ||
from users as u | ||
left join users as f on u.friend_id = f.id | ||
</select> | ||
|
||
<resultMap id="User" type="org.apache.ibatis.submitted.cursor_cache_oom.User"> | ||
<id property="id" column="id"/> | ||
<result property="name" column="name"/> | ||
<association property="friend" resultMap="Friend"/> | ||
</resultMap> | ||
|
||
<resultMap id="Friend" type="org.apache.ibatis.submitted.cursor_cache_oom.Friend"> | ||
<result property="name" column="friend_name"/> | ||
</resultMap> | ||
</mapper> |
42 changes: 42 additions & 0 deletions
42
src/test/resources/org/apache/ibatis/submitted/cursor_cache_oom/mybatis-config.xml
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,42 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!-- | ||
Copyright 2009-2022 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 | ||
https://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. | ||
--> | ||
<!DOCTYPE configuration | ||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN" | ||
"https://mybatis.org/dtd/mybatis-3-config.dtd"> | ||
|
||
<configuration> | ||
|
||
<environments default="development"> | ||
<environment id="development"> | ||
<transactionManager type="JDBC"> | ||
<property name="" value="" /> | ||
</transactionManager> | ||
<dataSource type="UNPOOLED"> | ||
<property name="driver" value="org.hsqldb.jdbcDriver" /> | ||
<property name="url" value="jdbc:hsqldb:mem:basetest" /> | ||
<property name="username" value="sa" /> | ||
</dataSource> | ||
</environment> | ||
</environments> | ||
|
||
<mappers> | ||
<mapper class="org.apache.ibatis.submitted.cursor_cache_oom.Mapper" /> | ||
</mappers> | ||
|
||
</configuration> |