About Explicit Locking

Ehcache contains an implementation which provides for explicit locking, using read and write locks.

With explicit locking, it is possible to get more control over Ehcache 's locking behavior to allow business logic to apply an atomic change with guaranteed ordering across one or more keys in one or more caches. It can therefore be used as a custom alternative to XA Transactions or Local transactions.

With that power comes a caution. It is possible to create deadlocks in your own business logic using this API.

Code Sample for Explicit Locking

The following is a simple example that shows how to use explicit locking.

String key = "123";
Foo val = new Foo();
cache.acquireWriteLockOnKey(key);
try {
cache.put(new Element(key, val));
} finally {
cache.releaseWriteLockOnKey(key);
}
...sometime later
String key = "123";
cache.acquireWriteLockOnKey(key);
try {
Object cachedVal = cache.get(key).getValue();
cachedVal.setSomething("abc");
cache.put(new Element(key, cachedVal));
} finally {
cache.releaseWriteLockOnKey(key);
}

How Locking Works

A READ lock does not prevent other READers from also acquiring a READ lock and reading. A READ lock cannot be obtained if there is an outstanding WRITE lock. It will queue.

A WRITE lock cannot be obtained while there are outstanding READ locks. It will queue.

In each case the lock should be released after use to avoid locking problems. The lock release should be in a “finally” block.

If before each read you acquire a READ lock and then before each write you acquire a WRITE lock, then an isolation level akin to READ_COMMITTED is achieved.

The Locking API

The following methods are available on Cache and Ehcache.

/**
* Acquires the proper read lock for a given cache key
*
* @param key - The key that retrieves a value that you want to protect via locking.
*/
public void acquireReadLockOnKey(Object key) {
this.acquireLockOnKey(key, LockType.READ);
}
/**
* Acquires the proper write lock for a given cache key
*
* @param key - The key that retrieves a value that you want to protect via locking.
*/
public void acquireWriteLockOnKey(Object key) {
this.acquireLockOnKey(key, LockType.WRITE);
}
/**
* Try to get a read lock on a given key. If can't get it in timeout millis
* then return a boolean telling that it didn't get the lock
*
* @param key - The key that retrieves a value that you want to protect via locking.
* @param timeout - millis until giveup on getting the lock
* @return whether the lock was awarded
* @throws InterruptedException
*/
public boolean tryReadLockOnKey(Object key, long timeout) throws InterruptedException {
Sync s = getLockForKey(key);
return s.tryLock(LockType.READ, timeout);
}
/**
* Try to get a write lock on a given key. If can't get it in timeout millis
* then return a boolean telling that it didn't get the lock
*
* @param key - The key that retrieves a value that you want to protect via locking.
* @param timeout - millis until giveup on getting the lock
* @return whether the lock was awarded
* @throws InterruptedException
*/
public boolean tryWriteLockOnKey(Object key, long timeout) throws InterruptedException {
Sync s = getLockForKey(key);
return s.tryLock(LockType.WRITE, timeout);
}
/**
* Release a held read lock for the passed in key
*
* @param key - The key that retrieves a value that you want to protect via locking.
*/
public void releaseReadLockOnKey(Object key) {
releaseLockOnKey(key, LockType.READ);
}
/**
* Release a held write lock for the passed in key
*
* @param key - The key that retrieves a value that you want to protect via
* locking.
*/
public void releaseWriteLockOnKey(Object key) {
releaseLockOnKey(key, LockType.WRITE);
}
/**
* Returns true if a read lock for the key is held by the current thread
*
* @param key
* @return true if a read lock for the key is held by the current thread
*/
boolean isReadLockedByCurrentThread(Object key);
/**
* Returns true if a write lock for the key is held by the current thread
*
* @param key
* @return true if a write lock for the key is held by the current thread
*/
boolean isWriteLockedByCurrentThread(Object key);

Supported Topologies

Except as noted in the The Locking API, the locking API supports the standalone and distributed cache topologies. It does not support the replicated topology.

Ehcache(2.9.x) - API Developer Guide, Using Explicit Locking的更多相关文章

  1. Ehcache(2.9.x) - API Developer Guide, Key Classes and Methods

    About the Key Classes Ehcache consists of a CacheManager, which manages logical data sets represente ...

  2. Ehcache(2.9.x) - API Developer Guide, Write-Through and Write-Behind Caches

    About Write-Through and Write-Behind Caches Write-through caching is a caching pattern where writes ...

  3. Ehcache(2.9.x) - API Developer Guide, Cache Eviction Algorithms

    About Cache Eviction Algorithms A cache eviction algorithm is a way of deciding which element to evi ...

  4. Ehcache(2.9.x) - API Developer Guide, Basic Caching

    Creating a CacheManager All usages of the Ehcache API start with the creation of a CacheManager. The ...

  5. Ehcache(2.9.x) - API Developer Guide, Cache Usage Patterns

    There are several common access patterns when using a cache. Ehcache supports the following patterns ...

  6. Ehcache(2.9.x) - API Developer Guide, Searching a Cache

    About Searching The Search API allows you to execute arbitrarily complex queries against caches. The ...

  7. Ehcache(2.9.x) - API Developer Guide, Transaction Support

    About Transaction Support Transactions are supported in versions of Ehcache 2.0 and higher. The 2.3. ...

  8. Ehcache(2.9.x) - API Developer Guide, Blocking and Self Populating Caches

    About Blocking and Self-Populating Caches The net.sf.ehcache.constructs package contains some applie ...

  9. Ehcache(2.9.x) - API Developer Guide, Cache Loaders

    About Cache Loaders A CacheLoader is an interface that specifies load() and loadAll() methods with a ...

随机推荐

  1. DotNetZip封装类

      DotnetZip是一个开源类库,支持.NET的任何语言,可很方便的创建,读取,和更新zip文件.而且还可以使用在.NETCompact Framework中. 下载地址在这里: http://d ...

  2. PHP之路,Day1 - PHP基础

    本节内容  1.PHP介绍  2.第一个PHP脚本程序  3.PHP语言标记  4.指令分割符  5.程序注释  6.在程序中使用空白符的处理  7.变量  8.变量的类型  9.数据类型之间相互转换 ...

  3. SQL 表锁(转)

    其实你可以使用事务处理   比方说在一个字段里面添加一个boolean 的字段当你要处理该字段的时候就 True 哪么别的人都不可以进行操作 如果是False 哪么就可以进行操作--呵可--我是这样的 ...

  4. UVa532 Dungeon Master 三维迷宫

        学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时)   #i ...

  5. Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理

    B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/pr ...

  6. mysql优化:连接数

    有时候我们会遇见"MySQL: ERROR 1040: Too many connections"的异常,一种原因是訪问量过高,MySQLserver抗不住,这个时候就要考虑添加从 ...

  7. 【zabbix系列】报警系统的设置和排除

    关于邮件报警,有非常多方案,这里选择的是稳定性较好.使用较多的msmtp+mutt方案. 该方案有一个非常好的地方在于不用自己来搭建独立的mailserver,能够使用第三方mail.这样的方法不仅能 ...

  8. 支持向量机(SVM)非线性数据切割

    支持向量机(SVM)非线性数据切割 1.目标 本指导中你将学到: l  当不可能线性切割训练数据时,如何定义SVM最优化问题. l  在这样的问题上.如何配置CvSVMParams中的參数满足你的SV ...

  9. C 语言 .h文件的作用

    C语言头文件的作用 最近在工作当中遇到了一点小问题,关于C语言头文件的应用问题,主要还是关于全局变量的定义和声明问题.学习C语言已经有好几年了,工作使用也近半年了,但是对于这部分的东西的确还没有深入的 ...

  10. hdu 3849 (双联通求桥)

    一道简单的双联通求桥的题目,,数据时字符串,,map用的不熟练啊,,,,,,,,,,,,, #include <iostream> #include <cstring> #in ...