Skip to content

Commit

Permalink
Related to mybatis#116. As per Iwao's suggestion, now removeObject is…
Browse files Browse the repository at this point in the history
… used to

notify the rollback to a blocking cache. EhCache adapter has been fixed
also.
  • Loading branch information
emacarron committed May 16, 2015
1 parent 6a5923b commit 6795085
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/main/java/org/apache/ibatis/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,18 @@ public interface Cache {
Object getObject(Object key);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* Simple blocking decorator
*
* Sipmle and inefficient version of EhCache's BlockingCache decorator.
* Simple and inefficient version of EhCache's BlockingCache decorator.
* It sets a lock over a cache key when the element is not found in cache.
* This way, other threads will wait until this element is filled instead of hitting the database.
*
Expand Down Expand Up @@ -76,7 +76,9 @@ public Object getObject(Object key) {

@Override
public Object removeObject(Object key) {
return delegate.removeObject(key);
// despite of its name, this method is called only to release locks
releaseLock(key);
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.concurrent.locks.ReadWriteLock;

import org.apache.ibatis.cache.Cache;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;

/**
* The 2nd level cache transactional buffer.
Expand All @@ -36,6 +38,8 @@
*/
public class TransactionalCache implements Cache {

private static final Log log = LogFactory.getLog(TransactionalCache.class);

private Cache delegate;
private boolean clearOnCommit;
private Map<Object, Object> entriesToAddOnCommit;
Expand Down Expand Up @@ -126,7 +130,12 @@ private void flushPendingEntries() {

private void unlockMissedEntries() {
for (Object entry : entriesMissedInCache) {
delegate.putObject(entry, null);
try {
delegate.removeObject(entry);
} catch (Exception e) {
log.warn("Unexpected exception while notifiying a rollback to the cache adapter."
+ "Consider upgrading your cache adapter to the latest version. Cause: " + e);
}
}
}

Expand Down

0 comments on commit 6795085

Please sign in to comment.