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