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 ...
随机推荐
- 前端的一些常用DOM和事件归纳
1.document.getElementById(id); document.getElementByTagName(tagName); document.getElementsByNa ...
- hdu 5745 La Vie en rose DP + bitset优化
http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...
- UVa Online Judge 工具網站
UVa Online Judge 工具網站 UVa中译题uHuntAlgorithmist Lucky貓的ACM園地,Lucky貓的 ACM 中譯題目 Mirror UVa Online Judg ...
- uistepper on ios versions prior to 5.0
xcode5 打开运行就出现这个错误 uistepper on ios versions prior to 5.0 直接在General -->Deployment Info -->Dep ...
- 单个SWF文件loading加载详解(转)
通过带宽查看器,可以看到SWF中每帧所占带宽状况.另外,我们还可以在Flash发布设置中,选择生成体积报告. 勾选这一项之后,发布flash时,会自动在fla目录中生成一个名为”文件名 Report. ...
- Java模拟登陆02【转载】
在使用java访问URL时,如果该URL需要身份验证,那么就不能够直接访问,因为没有登陆.那么,如何解决这个问题呢? 方法是使用java模拟登陆,登陆后记录下cookie信息,在下次发起请求时 ...
- Codeforces Gym 100203E E - bits-Equalizer 贪心
E - bits-EqualizerTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...
- 用C#.NET实现电子邮件客户程序
用C#.NET实现电子邮件客户程序 周华清 戴晟辉(东华理工学院计算机与通信系 江西 抚州 344000) [摘要]通过C#这种VisualSTudio.NET中新引入的面向对象且类型安全的编程语言, ...
- oracle 基本语句练习(一) where, between ,null, like,转义字符,order by
1 where 筛选关键字; select * from emp where job >'CLERK' 从emp表中列出 job值大于'CLERK'的项 (由于比较的是字符串,所以加单引号 ...
- 怎样破解邮箱password
破解邮箱password怎样破解邮箱password邮箱在我们的生活中日益成为一个不可或缺的角色.公司与公司之间的商贸往来,学生与老师间的学习交流,以及占非常大部分的私人信件的往来等等非常难离开它.但 ...