View Javadoc
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.heavy_initial_load;
17  
18  import java.io.Reader;
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.ibatis.io.Resources;
24  import org.apache.ibatis.session.SqlSession;
25  import org.apache.ibatis.session.SqlSessionFactory;
26  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.BeforeAll;
29  import org.junit.jupiter.api.Test;
30  
31  class HeavyInitialLoadTest {
32  
33    private static SqlSessionFactory sqlSessionFactory;
34  
35    @BeforeAll
36    static void initSqlSessionFactory() throws Exception {
37      try (Reader reader = Resources
38          .getResourceAsReader("org/apache/ibatis/submitted/heavy_initial_load/ibatisConfig.xml")) {
39        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
40      }
41  
42      sqlSessionFactory.getConfiguration().getEnvironment().getDataSource().getConnection().close();
43    }
44  
45    private static final int THREAD_COUNT = 5;
46  
47    /**
48     * Test to demonstrate the effect of the https://issues.apache.org/jira/browse/OGNL-121 issue in ognl on mybatis.
49     * <p>
50     * Use the thing mapper for the first time in multiple threads. The mapper contains a lot of ognl references to static
51     * final class members like:
52     * <p>
53     * <code>@org.apache.ibatis.submitted.heavy_initial_load.Code@_1.equals(code)</code>
54     * <p>
55     * Handling of these references is optimized in ognl (because they never change), but version 2.6.9 has a bug in
56     * caching the result . As a result the reference is translated to a 'null' value, which is used to invoke the
57     * 'equals' method on (hence the 'target is null for method equals' exception).
58     */
59    @Test
60    void selectThingsConcurrently_mybatis_issue_224() throws Exception {
61      final List<Throwable> throwables = Collections.synchronizedList(new ArrayList<>());
62  
63      Thread[] threads = new Thread[THREAD_COUNT];
64      for (int i = 0; i < THREAD_COUNT; i++) {
65        threads[i] = new Thread(() -> {
66          try {
67            selectThing();
68          } catch (Exception exception) {
69            throwables.add(exception);
70          }
71        });
72  
73        threads[i].start();
74      }
75  
76      for (int i = 0; i < THREAD_COUNT; i++) {
77        threads[i].join();
78      }
79  
80      Assertions.assertTrue(throwables.isEmpty(), "There were exceptions: " + throwables);
81    }
82  
83    void selectThing() {
84      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
85        ThingMapper mapper = sqlSession.getMapper(ThingMapper.class);
86        Thing selected = mapper.selectByCode(Code._1);
87        Assertions.assertEquals(1, selected.getId().longValue());
88      }
89    }
90  }