Ehcache(2.9.x) - API Developer Guide, Using Explicit Locking
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
随机推荐
- web.xml文件中配置ShallowEtagHeaderFilter需注意的问题
问题现象及解决方法 今天在Spring MVC应用中加入ShallowEtagHeaderFilter时,发现返回的响应中没有etag头,上网查了很多相关资料,也试了很多方法依然不起作用.在查看web ...
- INV(库存管理)
物料 PROCEDURE update_item(p_init_msg_list IN VARCHAR2 DEFAULT fnd_api.g_false, x_return_status OUT NO ...
- GitHub托管项目步骤
1.打开Git Shell ,进入你要托管的项目目录里.然后输入git init ,该项目下就会多一个.git文件夹 2.点击add,然后再path里面输入你项目的,git文件夹目录地址.如下: 3. ...
- MEF 编程指南(五):延迟导出
在组合部件的时候,导入将会触发部件(部件集合)的实例化,为原始的请求部件公开必要的导出需求.对于有些应用程序,推迟实例化 - 并且防止递归组合图(Recursive Composition Down ...
- PostgreSQL的 initdb 源代码分析之二十三
继续分析: vacuum_db(); 展开: cmd是:/home/pgsql/project/bin/postgres" --single -F -O -c search_path=pg_ ...
- ArcGIS Add-in开发(一)--获取选定要素的属性值
刚刚接触AE开发,记录一下自己的学习心得! 欢迎大家一起交流探讨! 最近做大赛,突然想到可以让项目更加直观的操作,就在项目中加了幅底图(底图很简单) 我想在arcmap中选中相应的要素后,在后台通过写 ...
- 【转】adns解析库——域名解析实例(C++、linux)
转自:http://blog.csdn.net/fty8788/article/details/7480334 adns是一个开源的dns解析库 官方文档:http://www.chiark.gree ...
- 支持向量机(SVM)非线性数据切割
支持向量机(SVM)非线性数据切割 1.目标 本指导中你将学到: l 当不可能线性切割训练数据时,如何定义SVM最优化问题. l 在这样的问题上.如何配置CvSVMParams中的參数满足你的SV ...
- 关于AutoComplete整合
AutoComplete应该不是很陌生了,网上也有好多开源的js.今天主要的不是研究Autocomplete这个js的实现.今天主要讲的是将这个js做成一插件.那么今天主要用到的 js插件jquery ...
- linux epoll模型
原文:http://yjtjh.blog.51cto.com/1060831/294119 Linux I/O多路复用技术在比较多的TCP网络服务器中有使用,即比较多的用到select函数.Linux ...