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.force_flush_on_select;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertNotSame;
20  
21  import java.io.Reader;
22  import java.sql.Connection;
23  import java.sql.SQLException;
24  import java.sql.Statement;
25  import java.util.List;
26  
27  import org.apache.ibatis.BaseDataTest;
28  import org.apache.ibatis.io.Resources;
29  import org.apache.ibatis.session.ExecutorType;
30  import org.apache.ibatis.session.LocalCacheScope;
31  import org.apache.ibatis.session.SqlSession;
32  import org.apache.ibatis.session.SqlSessionFactory;
33  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
34  import org.junit.jupiter.api.BeforeEach;
35  import org.junit.jupiter.api.Test;
36  
37  class ForceFlushOnSelectTest {
38  
39    private static SqlSessionFactory sqlSessionFactory;
40  
41    @BeforeEach
42    void initDatabase() throws Exception {
43      try (Reader reader = Resources
44          .getResourceAsReader("org/apache/ibatis/submitted/force_flush_on_select/ibatisConfig.xml")) {
45        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
46      }
47  
48      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
49          "org/apache/ibatis/submitted/force_flush_on_select/CreateDB.sql");
50    }
51  
52    @Test
53    void testShouldFlushLocalSessionCacheOnQuery() throws SQLException {
54      try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE)) {
55        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
56        personMapper.selectByIdFlush(1);
57        updateDatabase(sqlSession.getConnection());
58        Person updatedPerson = personMapper.selectByIdFlush(1);
59        assertEquals("Simone", updatedPerson.getFirstName());
60        sqlSession.commit();
61      }
62    }
63  
64    @Test
65    void testShouldNotFlushLocalSessionCacheOnQuery() throws SQLException {
66      try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE)) {
67        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
68        personMapper.selectByIdNoFlush(1);
69        updateDatabase(sqlSession.getConnection());
70        Person updatedPerson = personMapper.selectByIdNoFlush(1);
71        assertEquals("John", updatedPerson.getFirstName());
72        sqlSession.commit();
73      }
74    }
75  
76    @Test
77    void testShouldFlushLocalSessionCacheOnQueryForList() throws SQLException {
78      try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE)) {
79        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
80        List<Person> people = personMapper.selectAllFlush();
81        updateDatabase(sqlSession.getConnection());
82        people = personMapper.selectAllFlush();
83        assertEquals("Simone", people.get(0).getFirstName());
84        sqlSession.commit();
85      }
86    }
87  
88    @Test
89    void testShouldNotFlushLocalSessionCacheOnQueryForList() throws SQLException {
90      try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE)) {
91        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
92        List<Person> people = personMapper.selectAllNoFlush();
93        updateDatabase(sqlSession.getConnection());
94        people = personMapper.selectAllNoFlush();
95        assertEquals("John", people.get(0).getFirstName());
96        sqlSession.commit();
97      }
98    }
99  
100   private void updateDatabase(Connection conn) throws SQLException {
101     try (Statement stmt = conn.createStatement()) {
102       stmt.executeUpdate("UPDATE person SET firstName = 'Simone' WHERE id = 1");
103     }
104   }
105 
106   @Test
107   void testUpdateShouldFlushLocalCache() {
108     try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE)) {
109       PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
110       Person person = personMapper.selectByIdNoFlush(1);
111       person.setLastName("Perez"); // it is ignored in update
112       personMapper.update(person);
113       Person updatedPerson = personMapper.selectByIdNoFlush(1);
114       assertEquals("Smith", updatedPerson.getLastName());
115       assertNotSame(person, updatedPerson);
116       sqlSession.commit();
117     }
118   }
119 
120   @Test
121   void testSelectShouldFlushLocalCacheIfFlushLocalCacheAtferEachStatementIsTrue() throws SQLException {
122     sqlSessionFactory.getConfiguration().setLocalCacheScope(LocalCacheScope.STATEMENT);
123     try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE)) {
124       PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
125       List<Person> people = personMapper.selectAllNoFlush();
126       updateDatabase(sqlSession.getConnection());
127       people = personMapper.selectAllFlush();
128       assertEquals("Simone", people.get(0).getFirstName());
129       sqlSession.commit();
130     }
131   }
132 
133 }