db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/locking ReadCommittedStrategy.java ReadUncommittedStrategy.java SerializableStrategy.java AbstractLockStrategy.java RepeatableReadStrategy.java
Date Sat, 04 Dec 2004 13:47:54 GMT
arminw      2004/12/04 05:47:54

  Modified:    src/java/org/apache/ojb/broker/locking Tag: OJB_1_0_RELEASE
                        ReadCommittedStrategy.java
                        ReadUncommittedStrategy.java
                        SerializableStrategy.java AbstractLockStrategy.java
                        RepeatableReadStrategy.java
  Log:
  refactor classes, move common methods to abstract base class
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.1.2.3   +10 -79    db-ojb/src/java/org/apache/ojb/broker/locking/ReadCommittedStrategy.java
  
  Index: ReadCommittedStrategy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/locking/ReadCommittedStrategy.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- ReadCommittedStrategy.java	11 Nov 2004 23:04:19 -0000	1.1.2.2
  +++ ReadCommittedStrategy.java	4 Dec 2004 13:47:54 -0000	1.1.2.3
  @@ -33,6 +33,7 @@
    */
   public class ReadCommittedStrategy extends AbstractLockStrategy
   {
  +    // TODO: concurrent running problems possible
       public static final int ISOLATION_LEVEL = IsolationLevels.IL_READ_COMMITTED;
   
       public ReadCommittedStrategy(LockMap lockMap)
  @@ -59,13 +60,9 @@
                   return readLock(key, resourceId);
               }
           }
  -        if(writer.isOwnedBy(key))
  -        {
  -            return true;    // If I'm the writer, I can read.
  -        }
           else
           {
  -            return false;
  +            return writer.isOwnedBy(key);
           }
       }
   
  @@ -78,19 +75,12 @@
           // if there is no writer yet we can try to get the global write lock
           if(writer == null)
           {
  -            // if lock could be acquired return true
  -            if(setWriter(key, resourceId, ISOLATION_LEVEL))
  -                return true;
  -            // else try again
  -            else
  -                return writeLock(key, resourceId);
  +            return setWriter(key, resourceId, ISOLATION_LEVEL) ? true : writeLock(key,
resourceId);
           }
  -        if(writer.isOwnedBy(key))
  +        else
           {
  -            return true;    // If I'm the writer, then I can write.
  +            return writer.isOwnedBy(key);
           }
  -
  -        return false;
       }
   
       /**
  @@ -101,75 +91,16 @@
           LockEntry writer = getWriter(resourceId);
           if(writer == null)
           {
  -            // if lock could be acquired return true
  -            if(setWriter(key, resourceId, ISOLATION_LEVEL))
  -                return true;
  -            // else try again
  -            else
  -                return upgradeLock(key, resourceId);
  -        }
  -        if(writer.isOwnedBy(key))
  -        {
  -            return true;    // If I already have Write, then I've upgraded.
  -        }
  -
  -        return false;
  -    }
  -
  -    /**
  -     * @see LockStrategy#readLock(java.lang.Object, java.lang.Object)
  -     */
  -    public boolean releaseLock(Object key, Object resourceId)
  -    {
  -        LockEntry writer = getWriter(resourceId);
  -        boolean result = false;
  -//        if(writer != null && writer.isOwnedBy(key))
  -//        {
  -//            removeWriter(writer);
  -//            result = true;
  -//        }
  -//
  -//        if(hasReadLock(key, resourceId))
  -//        {
  -//            result = removeReader(key, resourceId);
  -//            result = true;
  -//        }
  -        if(writer != null && writer.isOwnedBy(key))
  -        {
  -            result = removeWriter(writer);
  +            return setWriter(key, resourceId, ISOLATION_LEVEL) ? true : upgradeLock(key,
resourceId);
           }
  -        if(removeReader(key, resourceId)) result = true;
  -        return result;
  -    }
  -
  -    /**
  -     * @see LockStrategy#checkRead(java.lang.Object, java.lang.Object)
  -     */
  -    public boolean checkRead(Object key, Object resourceId)
  -    {
  -        if(hasReadLock(key, resourceId))
  -        {
  -            return true;
  -        }
  -        LockEntry writer = getWriter(resourceId);
  -        if(writer != null && writer.isOwnedBy(key))
  +        else
           {
  -            return true;
  +            return writer.isOwnedBy(key);
           }
  -        return false;
       }
   
  -    /**
  -     * @see LockStrategy#checkWrite(java.lang.Object, java.lang.Object)
  -     */
  -    public boolean checkWrite(Object key, Object resourceId)
  +    int getIsolationLevel()
       {
  -        LockEntry writer = getWriter(resourceId);
  -        if(writer == null)
  -            return false;
  -        else if(writer.isOwnedBy(key))
  -            return true;
  -        else
  -            return false;
  +        return ISOLATION_LEVEL;
       }
   }
  
  
  
  1.1.2.3   +22 -33    db-ojb/src/java/org/apache/ojb/broker/locking/ReadUncommittedStrategy.java
  
  Index: ReadUncommittedStrategy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/locking/ReadUncommittedStrategy.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- ReadUncommittedStrategy.java	11 Nov 2004 23:04:19 -0000	1.1.2.2
  +++ ReadUncommittedStrategy.java	4 Dec 2004 13:47:54 -0000	1.1.2.3
  @@ -33,6 +33,7 @@
    */
   public class ReadUncommittedStrategy extends AbstractLockStrategy
   {
  +    // TODO: concurrent running problems possible
       public static final int ISOLATION_LEVEL = IsolationLevels.IL_READ_UNCOMMITTED;
   
       public ReadUncommittedStrategy(LockMap lockMap)
  @@ -49,57 +50,49 @@
       }
   
       /**
  -     * @see LockStrategy#writeLock(java.lang.Object, java.lang.Object)
  +     * Release read lock is always successful (because they will be ignored),
  +     * remove write lock behave normal (true if write lock is owned by caller/key).
        */
  -    public boolean writeLock(Object key, Object resourceId)
  +    public boolean releaseLock(Object key, Object resourceId)
       {
           LockEntry writer = getWriter(resourceId);
  -        if(writer == null)
  -        {
  -            if(setWriter(key, resourceId, ISOLATION_LEVEL))
  -                return true;
  -            else
  -                return writeLock(key, resourceId);
  -        }
  -        if(writer.isOwnedBy(key))
  +        if(writer != null && writer.isOwnedBy(key))
           {
  -            return true;    // If I'm the writer, then I can write.
  +            return removeWriter(writer);
           }
  -        return false;
  +        return true;
       }
   
       /**
  -     * @see LockStrategy#upgradeLock(java.lang.Object, java.lang.Object)
  +     * @see LockStrategy#writeLock(java.lang.Object, java.lang.Object)
        */
  -    public boolean upgradeLock(Object key, Object resourceId)
  +    public boolean writeLock(Object key, Object resourceId)
       {
           LockEntry writer = getWriter(resourceId);
           if(writer == null)
           {
  -            if(setWriter(key, resourceId, ISOLATION_LEVEL))
  -                return true;
  -            else
  -                return upgradeLock(key, resourceId);
  +            return setWriter(key, resourceId, ISOLATION_LEVEL) ? true : writeLock(key,
resourceId);
           }
  -        if(writer.isOwnedBy(key))
  +        else
           {
  -            return true;    // If I already have Write, then I've upgraded.
  +            return writer.isOwnedBy(key);
           }
  -        return false;
       }
   
       /**
  -     * @see LockStrategy#readLock(java.lang.Object, java.lang.Object)
  +     * @see LockStrategy#upgradeLock(java.lang.Object, java.lang.Object)
        */
  -    public boolean releaseLock(Object key, Object resourceId)
  +    public boolean upgradeLock(Object key, Object resourceId)
       {
           LockEntry writer = getWriter(resourceId);
  -        if(writer != null && writer.isOwnedBy(key))
  +        if(writer == null)
           {
  -            removeWriter(writer);
  +            return setWriter(key, resourceId, ISOLATION_LEVEL) ? true : upgradeLock(key,
resourceId);
  +        }
  +        else
  +        {
  +            return writer.isOwnedBy(key);
           }
  -        // readlocks cannot (and need not) be released, thus:
  -        return true;
       }
   
       /**
  @@ -110,12 +103,8 @@
           return checkWrite(key, resourceId);
       }
   
  -    /**
  -     * @see LockStrategy#checkWrite(java.lang.Object, java.lang.Object)
  -     */
  -    public boolean checkWrite(Object key, Object resourceId)
  +    int getIsolationLevel()
       {
  -        LockEntry writer = getWriter(resourceId);
  -        return (writer != null && writer.isOwnedBy(key));
  +        return ISOLATION_LEVEL;
       }
   }
  
  
  
  1.1.2.3   +22 -83    db-ojb/src/java/org/apache/ojb/broker/locking/SerializableStrategy.java
  
  Index: SerializableStrategy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/locking/SerializableStrategy.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- SerializableStrategy.java	11 Nov 2004 23:04:19 -0000	1.1.2.2
  +++ SerializableStrategy.java	4 Dec 2004 13:47:54 -0000	1.1.2.3
  @@ -27,6 +27,7 @@
    */
   public class SerializableStrategy extends AbstractLockStrategy
   {
  +    // TODO: concurrent running problems possible
       public static final int ISOLATION_LEVEL = IsolationLevels.IL_SERIALIZABLE;
   
       public SerializableStrategy(LockMap lockMap)
  @@ -40,9 +41,9 @@
       public boolean readLock(Object key, Object resourceId)
       {
           LockEntry writer = getWriter(resourceId);
  -        Collection readers = getReaders(resourceId);
           if(writer == null)
           {
  +            Collection readers = getReaders(resourceId);
               // only one reader at a time
               if(readers.size() == 0)
               {
  @@ -62,18 +63,16 @@
                   else
                       return readLock(key, resourceId);
               }
  -            else if((readers.size() == 1) && (((LockEntry) readers.iterator().next()).isOwnedBy(key)))
  +            else
               {
                   // I'm the reader, thus I am allowed to read even more !
  -                return true;
  +                return (readers.size() == 1) && (((LockEntry) readers.iterator().next()).isOwnedBy(key));
               }
           }
  -        else if(writer.isOwnedBy(key))
  +        else
           {
  -            return true;    // If I'm the writer, I can read.
  +            return writer.isOwnedBy(key);
           }
  -
  -        return false;
       }
   
       /**
  @@ -87,23 +86,21 @@
           {
               if(readers.size() == 0)
               {
  -                if(setWriter(key, resourceId, ISOLATION_LEVEL))
  -                    return true;
  -                else
  -                    return writeLock(key, resourceId);
  +                return setWriter(key, resourceId, ISOLATION_LEVEL) ? true : writeLock(key,
resourceId);
               }
  -
               else if(readers.size() == 1)
               {
  -                if(((LockEntry) readers.iterator().next()).isOwnedBy(key))
  -                    return upgradeLock(key, resourceId);
  +                return ((LockEntry) readers.iterator().next()).isOwnedBy(key) ? upgradeLock(key,
resourceId) : false;
  +            }
  +            else
  +            {
  +                return false;
               }
           }
  -        else if(writer.isOwnedBy(key))
  +        else
           {
  -            return true;    // If I'm the writer, then I can write.
  +            return writer.isOwnedBy(key);
           }
  -        return false;
       }
   
       /**
  @@ -114,87 +111,29 @@
           LockEntry writer = getWriter(resourceId);
           if(writer == null)
           {
  -            Collection readers = getReaders(resourceId);
  +            Collection readers = this.getReaders(resourceId);
               if(readers.size() == 1)
               {
                   LockEntry reader = (LockEntry) readers.iterator().next();
                   if(reader.isOwnedBy(key))
                   {
  -                    if(upgradeLock(reader))
  -                        return true;
  -                    else
  -                        return upgradeLock(key, resourceId);
  +                    return upgradeLock(reader) ? true : upgradeLock(key, resourceId);
                   }
               }
  -            else
  +            else if(readers.size() == 0)
               {
  -                if(readers.size() == 0)
  -                {
  -                    if(setWriter(key, resourceId, ISOLATION_LEVEL))
  -                        return true;
  -                    else
  -                        return upgradeLock(key, resourceId);
  -                }
  +                return setWriter(key, resourceId, ISOLATION_LEVEL) ? true : upgradeLock(key,
resourceId);
               }
           }
  -        else if(writer.isOwnedBy(key))
  +        else
           {
  -            return true;    // If I already have Write, then I've upgraded.
  +            return writer.isOwnedBy(key);
           }
  -
           return false;
       }
   
  -    /**
  -     * @see LockStrategy#readLock(java.lang.Object, java.lang.Object)
  -     */
  -    public boolean releaseLock(Object key, Object resourceId)
  -    {
  -        LockEntry writer = getWriter(resourceId);
  -        boolean result = false;
  -//        if(writer != null && writer.isOwnedBy(key))
  -//        {
  -//            removeWriter(writer);
  -//            result = true;
  -//        }
  -//
  -//        if(hasReadLock(key, resourceId))
  -//        {
  -//            removeReader(key, resourceId);
  -//            result = true;
  -//        }
  -        if(writer != null && writer.isOwnedBy(key))
  -        {
  -            result = removeWriter(writer);
  -        }
  -        if(removeReader(key, resourceId)) result = true;
  -        return result;
  -    }
  -
  -    /**
  -     * @see LockStrategy#checkRead(java.lang.Object, java.lang.Object)
  -     */
  -    public boolean checkRead(Object key, Object resourceId)
  +    int getIsolationLevel()
       {
  -        if(hasReadLock(key, resourceId))
  -        {
  -            return true;
  -        }
  -        LockEntry writer = getWriter(resourceId);
  -        if(writer != null && writer.isOwnedBy(key))
  -        {
  -            return true;
  -        }
  -        else
  -            return false;
  -    }
  -
  -    /**
  -     * @see LockStrategy#checkWrite(java.lang.Object, java.lang.Object)
  -     */
  -    public boolean checkWrite(Object key, Object resourceId)
  -    {
  -        LockEntry writer = getWriter(resourceId);
  -        return (writer != null && writer.isOwnedBy(key));
  +        return ISOLATION_LEVEL;
       }
   }
  
  
  
  1.1.2.3   +49 -0     db-ojb/src/java/org/apache/ojb/broker/locking/AbstractLockStrategy.java
  
  Index: AbstractLockStrategy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/locking/AbstractLockStrategy.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- AbstractLockStrategy.java	11 Nov 2004 23:04:19 -0000	1.1.2.2
  +++ AbstractLockStrategy.java	4 Dec 2004 13:47:54 -0000	1.1.2.3
  @@ -108,4 +108,53 @@
       {
           return lockMap.hasReadLock(key, resourceId);
       }
  +
  +    /**
  +     * Returns the lock strategy isolation level.
  +     *
  +     * @return
  +     */
  +    abstract int getIsolationLevel();
  +
  +    /**
  +     * @see LockStrategy#checkWrite(java.lang.Object, java.lang.Object)
  +     */
  +    public boolean checkWrite(Object key, Object resourceId)
  +    {
  +        LockEntry writer = getWriter(resourceId);
  +        return (writer != null && writer.isOwnedBy(key));
  +    }
  +
  +    /**
  +     * @see LockStrategy#checkRead(java.lang.Object, java.lang.Object)
  +     */
  +    public boolean checkRead(Object key, Object resourceId)
  +    {
  +        if(hasReadLock(key, resourceId))
  +        {
  +            return true;
  +        }
  +        LockEntry writer = getWriter(resourceId);
  +        if(writer != null && writer.isOwnedBy(key))
  +        {
  +            return true;
  +        }
  +        else
  +            return false;
  +    }
  +
  +    /**
  +     * @see LockStrategy#readLock(java.lang.Object, java.lang.Object)
  +     */
  +    public boolean releaseLock(Object key, Object resourceId)
  +    {
  +        LockEntry writer = getWriter(resourceId);
  +        boolean result = false;
  +        if(writer != null && writer.isOwnedBy(key))
  +        {
  +            result = removeWriter(writer);
  +        }
  +        if(removeReader(key, resourceId)) result = true;
  +        return result;
  +    }
   }
  
  
  
  1.1.2.3   +19 -82    db-ojb/src/java/org/apache/ojb/broker/locking/RepeatableReadStrategy.java
  
  Index: RepeatableReadStrategy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/locking/RepeatableReadStrategy.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- RepeatableReadStrategy.java	11 Nov 2004 23:04:19 -0000	1.1.2.2
  +++ RepeatableReadStrategy.java	4 Dec 2004 13:47:54 -0000	1.1.2.3
  @@ -31,6 +31,7 @@
    */
   public class RepeatableReadStrategy extends AbstractLockStrategy
   {
  +    // TODO: concurrent running problems possible
       public static final int ISOLATION_LEVEL = IsolationLevels.IL_REPEATABLE_READ;
   
       public RepeatableReadStrategy(LockMap lockMap)
  @@ -46,18 +47,12 @@
           LockEntry writer = getWriter(resourceId);
           if(writer == null)
           {
  -            if(addReader(key, resourceId, ISOLATION_LEVEL))
  -                return true;
  -            else
  -                return readLock(key, resourceId);
  +            return addReader(key, resourceId, ISOLATION_LEVEL) ? true : readLock(key, resourceId);
           }
  -        if(writer.isOwnedBy(key))
  +        else
           {
  -            return true;    // If I'm the writer, I can read.
  +            return writer.isOwnedBy(key);
           }
  -        else
  -            return false;
  -
       }
   
       /**
  @@ -66,28 +61,26 @@
       public boolean writeLock(Object key, Object resourceId)
       {
           LockEntry writer = getWriter(resourceId);
  -        Collection readers = getReaders(resourceId);
           if(writer == null)
           {
  +            Collection readers = getReaders(resourceId);
               if(readers.size() == 0)
               {
  -                if(setWriter(key, resourceId, ISOLATION_LEVEL))
  -                    return true;
  -                else
  -                    return writeLock(key, resourceId);
  +                return setWriter(key, resourceId, ISOLATION_LEVEL) ? true : writeLock(key,
resourceId);
               }
  -
               else if(readers.size() == 1)
               {
  -                if(((LockEntry) readers.iterator().next()).isOwnedBy(key))
  -                    return upgradeLock(key, resourceId);
  +                return ((LockEntry) readers.iterator().next()).isOwnedBy(key) ? upgradeLock(key,
resourceId) : false;
  +            }
  +            else
  +            {
  +                return false;
               }
           }
  -        else if(writer.isOwnedBy(key))
  +        else
           {
  -            return true;    // If I'm the writer, then I can write.
  +            return writer.isOwnedBy(key);
           }
  -        return false;
       }
   
   
  @@ -105,79 +98,23 @@
                   LockEntry reader = (LockEntry) readers.iterator().next();
                   if(reader.isOwnedBy(key))
                   {
  -                    if(upgradeLock(reader))
  -                        return true;
  -                    else
  -                        return upgradeLock(key, resourceId);
  +                    return upgradeLock(reader) ? true : upgradeLock(key, resourceId);
                   }
               }
               else if(readers.size() == 0)
               {
  -                if(setWriter(key, resourceId, ISOLATION_LEVEL))
  -                    return true;
  -                else
  -                    return upgradeLock(key, resourceId);
  +                return setWriter(key, resourceId, ISOLATION_LEVEL) ? true : upgradeLock(key,
resourceId);
               }
  -
  -
           }
  -        else if(writer.isOwnedBy(key))
  +        else
           {
  -            return true;    // If I already have Write, then I've upgraded.
  +            return writer.isOwnedBy(key);
           }
  -
           return false;
       }
   
  -    /**
  -     * @see LockStrategy#readLock(java.lang.Object, java.lang.Object)
  -     */
  -    public boolean releaseLock(Object key, Object resourceId)
  +    int getIsolationLevel()
       {
  -        LockEntry writer = getWriter(resourceId);
  -        boolean result = false;
  -//        if(writer != null && writer.isOwnedBy(key))
  -//        {
  -//            removeWriter(writer);
  -//            result = true;
  -//        }
  -//        if(hasReadLock(key, resourceId))
  -//        {
  -//            removeReader(key, resourceId);
  -//            result = true;
  -//        }
  -        if(writer != null && writer.isOwnedBy(key))
  -        {
  -            result = removeWriter(writer);
  -        }
  -        if(removeReader(key, resourceId)) result = true;
  -        return result;
  -    }
  -
  -    /**
  -     * @see LockStrategy#checkRead(java.lang.Object, java.lang.Object)
  -     */
  -    public boolean checkRead(Object key, Object resourceId)
  -    {
  -        if(hasReadLock(key, resourceId))
  -        {
  -            return true;
  -        }
  -        LockEntry writer = getWriter(resourceId);
  -        if(writer != null && writer.isOwnedBy(key))
  -        {
  -            return true;
  -        }
  -        else
  -            return false;
  -    }
  -
  -    /**
  -     * @see LockStrategy#checkWrite(java.lang.Object, java.lang.Object)
  -     */
  -    public boolean checkWrite(Object key, Object resourceId)
  -    {
  -        LockEntry writer = getWriter(resourceId);
  -        return (writer != null && writer.isOwnedBy(key));
  +        return ISOLATION_LEVEL;
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message