KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > lock > LockStrategyReadUncommitted


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.lock;
8
9 import java.util.concurrent.TimeUnit JavaDoc;
10 import java.util.concurrent.locks.Lock JavaDoc;
11
12 /**
13  * Transaction isolation level of READ-UNCOMMITTED. Reads always succeed (NullLock), whereas writes are exclusive.
14  * It prevents none of the dirty read, non-repeatable read, or phantom read.
15  *
16  * @author Ben Wang
17  * @version $Revision: 1.2 $
18  */

19 public class LockStrategyReadUncommitted implements LockStrategy
20 {
21    private SemaphoreLock wLock_;
22    private Lock JavaDoc rLock_; // Null lock will always return true
23

24    public LockStrategyReadUncommitted()
25    {
26       wLock_ = new SemaphoreLock(1);
27       rLock_ = new NullLock();
28    }
29    
30    /**
31     * @see org.jboss.cache.lock.LockStrategy#readLock()
32     */

33    public Lock JavaDoc readLock()
34    {
35       return rLock_;
36    }
37
38    /**
39     * @see org.jboss.cache.lock.LockStrategy#upgradeLockAttempt(long)
40     */

41    public Lock JavaDoc upgradeLockAttempt(long msecs) throws UpgradeException
42    {
43       // Since write is exclusive, we need to obtain the write lock first
44
// before we can return the upgrade
45
try {
46          wLock_.tryLock(msecs, TimeUnit.MILLISECONDS);
47       } catch (InterruptedException JavaDoc e) {
48          throw new UpgradeException("Upgrade failed in " + msecs + " msecs", e);
49       }
50       return wLock_;
51    }
52
53    /**
54     * @see org.jboss.cache.lock.LockStrategy#writeLock()
55     */

56    public Lock JavaDoc writeLock()
57    {
58       return wLock_;
59    }
60 }
61
Popular Tags