View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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.blocking_cache;
17  
18  import java.io.Reader;
19  import java.util.concurrent.ExecutorService;
20  import java.util.concurrent.Executors;
21  
22  import org.apache.ibatis.BaseDataTest;
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.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  // issue #524
32  class BlockingCacheTest {
33  
34    private static SqlSessionFactory sqlSessionFactory;
35  
36    @BeforeEach
37    void setUp() throws Exception {
38      // create a SqlSessionFactory
39      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/blocking_cache/mybatis-config.xml")) {
40        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41      }
42  
43      // populate in-memory database
44      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
45              "org/apache/ibatis/submitted/blocking_cache/CreateDB.sql");
46    }
47  
48    @Test
49    void testBlockingCache() {
50      ExecutorService defaultThreadPool = Executors.newFixedThreadPool(2);
51  
52      long init = System.currentTimeMillis();
53  
54      for (int i = 0; i < 2; i++) {
55        defaultThreadPool.execute(this::accessDB);
56      }
57  
58      defaultThreadPool.shutdown();
59  
60      while (!defaultThreadPool.isTerminated()) {
61      }
62  
63      long totalTime = System.currentTimeMillis() - init;
64      Assertions.assertTrue(totalTime > 1000);
65    }
66  
67    private void accessDB() {
68      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
69        PersonMapper pm = sqlSession.getMapper(PersonMapper.class);
70        pm.findAll();
71        try {
72          Thread.sleep(500);
73        } catch (InterruptedException e) {
74          Assertions.fail(e.getMessage());
75        }
76      }
77    }
78  
79    @Test
80    void ensureLockIsAcquiredBeforePut() {
81      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
82        PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
83        mapper.delete(-1);
84        mapper.findAll();
85        sqlSession.commit();
86      }
87    }
88  
89    @Test
90    void ensureLockIsReleasedOnRollback() {
91      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
92        PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
93        mapper.delete(-1);
94        mapper.findAll();
95        sqlSession.rollback();
96      }
97      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
98        PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
99        mapper.findAll();
100     }
101   }
102 }