Skip to content

Commit d698fb0

Browse files
committed
Fixed the flush ability of select mappers. They will now clear the local internal session cache when flushCache="true".
1 parent c23d6d7 commit d698fb0

File tree

7 files changed

+209
-0
lines changed

7 files changed

+209
-0
lines changed

src/main/java/org/apache/ibatis/executor/BaseExecutor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ public List<BatchResult> flushStatements(boolean isRollBack) throws SQLException
101101
public List query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
102102
ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());
103103
if (closed) throw new ExecutorException("Executor was closed.");
104+
105+
// Flush the internal cache is force is true
106+
if (ms.isFlushCacheRequired()) {
107+
clearLocalCache();
108+
}
104109
List list;
105110
try {
106111
queryStack++;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
create table person (
2+
id int,
3+
firstName varchar(100),
4+
lastName varchar(100)
5+
);
6+
7+
INSERT INTO person (id, firstName, lastName)
8+
VALUES (1, 'John', 'Smith');
9+
10+
INSERT INTO person (id, firstName, lastName)
11+
VALUES (2, 'Christian', 'Poitras');
12+
13+
INSERT INTO person (id, firstName, lastName)
14+
VALUES (3, 'Clinton', 'Begin');
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.apache.ibatis.submitted.force_flush_on_select;
2+
3+
import java.io.Reader;
4+
import java.sql.Connection;
5+
import java.sql.DriverManager;
6+
import java.util.List;
7+
8+
import org.apache.ibatis.io.Resources;
9+
import org.apache.ibatis.jdbc.ScriptRunner;
10+
import org.apache.ibatis.session.ExecutorType;
11+
import org.apache.ibatis.session.SqlSession;
12+
import org.apache.ibatis.session.SqlSessionFactory;
13+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
14+
import org.junit.BeforeClass;
15+
import org.junit.Test;
16+
17+
import static org.junit.Assert.assertEquals;
18+
19+
public class ForceFlushOnSelectTest {
20+
21+
private static SqlSessionFactory sqlSessionFactory;
22+
23+
@BeforeClass
24+
public static void initDatabase() throws Exception {
25+
Connection conn = null;
26+
27+
try {
28+
Class.forName("org.hsqldb.jdbcDriver");
29+
conn = DriverManager.getConnection("jdbc:hsqldb:mem:force_flush_on_select", "sa", "");
30+
31+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/force_flush_on_select/CreateDB.sql");
32+
33+
ScriptRunner runner = new ScriptRunner(conn);
34+
runner.setLogWriter(null);
35+
runner.setErrorLogWriter(null);
36+
runner.runScript(reader);
37+
conn.commit();
38+
reader.close();
39+
40+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/force_flush_on_select/ibatisConfig.xml");
41+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42+
reader.close();
43+
} finally {
44+
if (conn != null) {
45+
conn.close();
46+
}
47+
}
48+
}
49+
50+
@Test
51+
public void testShouldFlushLocalSessionCacheOnQuery() {
52+
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
53+
try {
54+
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
55+
Person person = personMapper.selectById(1);
56+
person.setFirstName("Simone");
57+
58+
personMapper.update(person);
59+
60+
Person updatedPerson = personMapper.selectById(1);
61+
62+
assertEquals("Simone", updatedPerson.getFirstName());
63+
64+
sqlSession.commit();
65+
} finally {
66+
sqlSession.close();
67+
}
68+
}
69+
70+
@Test
71+
public void testShouldFlushLocalSessionCacheOnQueryForList() {
72+
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
73+
try {
74+
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
75+
List<Person> people = personMapper.selectAll();
76+
77+
Person person = people.get(0);
78+
person.setFirstName("Simone");
79+
80+
personMapper.update(person);
81+
82+
people = personMapper.selectAll();
83+
84+
assertEquals("Simone", people.get(0).getFirstName());
85+
86+
sqlSession.commit();
87+
} finally {
88+
sqlSession.close();
89+
}
90+
}
91+
92+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.apache.ibatis.submitted.force_flush_on_select;
2+
3+
import java.io.Serializable;
4+
5+
public class Person implements Serializable {
6+
private Integer id;
7+
private String firstName;
8+
private String lastName;
9+
public String getFirstName() {
10+
return firstName;
11+
}
12+
public void setFirstName(String firstName) {
13+
this.firstName = firstName;
14+
}
15+
public String getLastName() {
16+
return lastName;
17+
}
18+
public void setLastName(String lastName) {
19+
this.lastName = lastName;
20+
}
21+
public Integer getId() {
22+
return id;
23+
}
24+
public void setId(Integer id) {
25+
this.id = id;
26+
}
27+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<!DOCTYPE mapper
3+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5+
6+
<mapper namespace="org.apache.ibatis.submitted.force_flush_on_select.PersonMapper">
7+
8+
<!-- One hour cache -->
9+
<cache flushInterval="3600000"/>
10+
11+
<resultMap id="personMap" type="Person">
12+
<id property="id" column="id"/>
13+
<result property="firstName" column="firstName"/>
14+
<result property="lastName" column="lastName"/>
15+
</resultMap>
16+
17+
18+
<select id="selectById" resultMap="personMap" parameterType="int">
19+
SELECT id, firstName, lastName
20+
FROM person
21+
WHERE id = #{id}
22+
</select>
23+
24+
<select id="selectAll" resultMap="personMap">
25+
SELECT id, firstName, lastName
26+
FROM person
27+
ORDER BY id
28+
</select>
29+
30+
<!-- normally this would be a stored procedure or a function -->
31+
<select id="update" parameterType="Person" resultType="Person" flushCache="true">
32+
UPDATE person
33+
SET firstName = #{firstName},
34+
lastName = #{lastName}
35+
WHERE id = #{id}
36+
</select>
37+
</mapper>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.apache.ibatis.submitted.force_flush_on_select;
2+
3+
import java.util.List;
4+
5+
public interface PersonMapper {
6+
public Person selectById(int id);
7+
public List<Person> selectAll();
8+
public void update(Person person);
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<!DOCTYPE configuration
3+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
5+
6+
<configuration>
7+
<typeAliases>
8+
<typeAlias alias="Person" type="org.apache.ibatis.submitted.force_flush_on_select.Person"/>
9+
</typeAliases>
10+
11+
<environments default="test">
12+
<environment id="test">
13+
<transactionManager type="JDBC"></transactionManager>
14+
<dataSource type="UNPOOLED">
15+
<property name="driver" value="org.hsqldb.jdbcDriver"/>
16+
<property name="url" value="jdbc:hsqldb:mem:force_flush_on_select"/>
17+
<property name="username" value="sa"/>
18+
</dataSource>
19+
</environment>
20+
</environments>
21+
22+
<mappers>
23+
<mapper resource="org/apache/ibatis/submitted/force_flush_on_select/Person.xml"/>
24+
</mappers>
25+
</configuration>

0 commit comments

Comments
 (0)