Skip to content

Commit 7dde13f

Browse files
committed
1 parent 7ea5da8 commit 7dde13f

File tree

4 files changed

+52
-20
lines changed

4 files changed

+52
-20
lines changed

src/main/java/org/apache/ibatis/cache/TransactionalCacheManager.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2012 The MyBatis Team
2+
* Copyright 2009-2011 The MyBatis Team
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,24 +23,13 @@
2323
public class TransactionalCacheManager {
2424

2525
private Map<Cache, TransactionalCache> transactionalCaches = new HashMap<Cache, TransactionalCache>();
26-
private boolean autoCommit; // issue #573. No need to call commit() on autoCommit sessions
27-
28-
public TransactionalCacheManager(){
29-
this(false);
30-
}
31-
32-
public TransactionalCacheManager(boolean autoCommit) {
33-
this.autoCommit = autoCommit;
34-
}
35-
26+
3627
public void clear(Cache cache) {
3728
getTransactionalCache(cache).clear();
38-
if (autoCommit) commit();
3929
}
4030

4131
public void putObject(Cache cache, CacheKey key, Object value) {
4232
getTransactionalCache(cache).putObject(key, value);
43-
if (autoCommit) commit();
4433
}
4534

4635
public void commit() {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class CachingExecutor implements Executor {
3535

3636
private Executor delegate;
3737
private boolean autoCommit; // issue #573. No need to call commit() on autoCommit sessions
38-
private TransactionalCacheManager tcm;
38+
private TransactionalCacheManager tcm = new TransactionalCacheManager();
3939

4040
private boolean dirty;
4141

@@ -46,7 +46,6 @@ public CachingExecutor(Executor delegate) {
4646
public CachingExecutor(Executor delegate, boolean autoCommit) {
4747
this.delegate = delegate;
4848
this.autoCommit = autoCommit;
49-
this.tcm = new TransactionalCacheManager(autoCommit);
5049
}
5150

5251
public Transaction getTransaction() {
@@ -154,9 +153,11 @@ public void clearLocalCache() {
154153

155154
private void flushCacheIfRequired(MappedStatement ms) {
156155
Cache cache = ms.getCache();
157-
if (cache != null && ms.isFlushCacheRequired()) {
158-
tcm.clear(cache);
159-
if (!autoCommit) dirty = true; // issue #524. Disable using cached data for this session because data may have changed
156+
if (cache != null) {
157+
if (ms.isFlushCacheRequired()) {
158+
if (!autoCommit) dirty = true; // issue #524. Disable using cached data for this session
159+
tcm.clear(cache);
160+
}
160161
}
161162
}
162163

src/test/java/org/apache/ibatis/submitted/cache/CacheTest.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void testplan1() {
9797
* Step 6 returns 2 rows.
9898
*/
9999
@Test
100-
public void testplan5() {
100+
public void testplan2() {
101101
SqlSession sqlSession1 = sqlSessionFactory.openSession(false);
102102
try {
103103
PersonMapper pm = sqlSession1.getMapper(PersonMapper.class);
@@ -126,5 +126,47 @@ public void testplan5() {
126126
sqlSession3.close();
127127
}
128128
}
129+
130+
/*
131+
* Test Plan with Autocommit on:
132+
* 1) SqlSession 1 executes "select * from A".
133+
* 2) SqlSession 1 closes.
134+
* 3) SqlSession 2 executes "delete from A where id = 1"
135+
* 4) SqlSession 2 closes.
136+
* 5) SqlSession 2 executes "select * from A".
137+
* 6) SqlSession 3 closes.
138+
*
139+
* Assert:
140+
* Step 6 returns 1 row.
141+
*/
142+
@Test
143+
public void testplan3() {
144+
SqlSession sqlSession1 = sqlSessionFactory.openSession(true);
145+
try {
146+
PersonMapper pm = sqlSession1.getMapper(PersonMapper.class);
147+
Assert.assertEquals(2, pm.findAll().size());
148+
}
149+
finally {
150+
sqlSession1.close();
151+
}
152+
153+
SqlSession sqlSession2 = sqlSessionFactory.openSession(true);
154+
try {
155+
PersonMapper pm = sqlSession2.getMapper(PersonMapper.class);
156+
pm.delete(1);
157+
}
158+
finally {
159+
sqlSession2.close();
160+
}
161+
162+
SqlSession sqlSession3 = sqlSessionFactory.openSession(true);
163+
try {
164+
PersonMapper pm = sqlSession3.getMapper(PersonMapper.class);
165+
Assert.assertEquals(1, pm.findAll().size());
166+
}
167+
finally {
168+
sqlSession3.close();
169+
}
170+
}
129171

130172
}

src/test/java/org/apache/ibatis/submitted/cache/CreateDB.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
-- limitations under the License.
1515
--
1616

17-
drop table person;
17+
drop table person if exists;
1818

1919
create table person(
2020
id int,

0 commit comments

Comments
 (0)