Skip to content

Commit 6795085

Browse files
committed
Related to #116. As per Iwao's suggestion, now removeObject is used to
notify the rollback to a blocking cache. EhCache adapter has been fixed also.
1 parent 6a5923b commit 6795085

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,18 @@ public interface Cache {
5959
Object getObject(Object key);
6060

6161
/**
62-
* Optional. It is not called by the core.
62+
* As of 3.3.0 this method is only called during a rollback
63+
* for any previous value that was missing in the cache.
64+
* This lets any blocking cache to release the lock that
65+
* may have previously put on the key.
66+
* A blocking cache puts a lock when a value is null
67+
* and releases it when the value is back again.
68+
* This way other threads will wait for the value to be
69+
* available instead of hitting the database.
70+
*
6371
*
6472
* @param key The key
65-
* @return The object that was removed
73+
* @return Not used
6674
*/
6775
Object removeObject(Object key);
6876

src/main/java/org/apache/ibatis/cache/decorators/BlockingCache.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* Simple blocking decorator
2929
*
30-
* Sipmle and inefficient version of EhCache's BlockingCache decorator.
30+
* Simple and inefficient version of EhCache's BlockingCache decorator.
3131
* It sets a lock over a cache key when the element is not found in cache.
3232
* This way, other threads will wait until this element is filled instead of hitting the database.
3333
*
@@ -76,7 +76,9 @@ public Object getObject(Object key) {
7676

7777
@Override
7878
public Object removeObject(Object key) {
79-
return delegate.removeObject(key);
79+
// despite of its name, this method is called only to release locks
80+
releaseLock(key);
81+
return null;
8082
}
8183

8284
@Override

src/main/java/org/apache/ibatis/cache/decorators/TransactionalCache.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.concurrent.locks.ReadWriteLock;
2323

2424
import org.apache.ibatis.cache.Cache;
25+
import org.apache.ibatis.logging.Log;
26+
import org.apache.ibatis.logging.LogFactory;
2527

2628
/**
2729
* The 2nd level cache transactional buffer.
@@ -36,6 +38,8 @@
3638
*/
3739
public class TransactionalCache implements Cache {
3840

41+
private static final Log log = LogFactory.getLog(TransactionalCache.class);
42+
3943
private Cache delegate;
4044
private boolean clearOnCommit;
4145
private Map<Object, Object> entriesToAddOnCommit;
@@ -126,7 +130,12 @@ private void flushPendingEntries() {
126130

127131
private void unlockMissedEntries() {
128132
for (Object entry : entriesMissedInCache) {
129-
delegate.putObject(entry, null);
133+
try {
134+
delegate.removeObject(entry);
135+
} catch (Exception e) {
136+
log.warn("Unexpected exception while notifiying a rollback to the cache adapter."
137+
+ "Consider upgrading your cache adapter to the latest version. Cause: " + e);
138+
}
130139
}
131140
}
132141

0 commit comments

Comments
 (0)