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