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/util/sequence SequenceManagerHighLowImpl.java HighLowSequence.java
Date Wed, 03 Sep 2003 18:45:29 GMT
arminw      2003/09/03 11:45:29

  Modified:    src/java/org/apache/ojb/broker/util/sequence
                        SequenceManagerHighLowImpl.java
                        HighLowSequence.java
  Log:
  fix bug, SM block application in
  heavy multithreaded loading.
  Remove the intern use of a new
  PB instance.
  
  Revision  Changes    Path
  1.21      +55 -49    db-ojb/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerHighLowImpl.java
  
  Index: SequenceManagerHighLowImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerHighLowImpl.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- SequenceManagerHighLowImpl.java	13 Aug 2003 18:16:10 -0000	1.20
  +++ SequenceManagerHighLowImpl.java	3 Sep 2003 18:45:29 -0000	1.21
  @@ -55,9 +55,8 @@
    */
   
   import org.apache.commons.lang.SystemUtils;
  -import org.apache.ojb.broker.PersistenceBroker;
  -import org.apache.ojb.broker.PersistenceBrokerFactory;
   import org.apache.ojb.broker.OptimisticLockException;
  +import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
  @@ -152,11 +151,12 @@
   
   
       private static Map sequencesMap = new HashMap();
  +    private static int attempts;
   
       protected boolean useGlobalSequenceIdentities;
       protected int grabSize;
       protected long globalSequenceStart;
  -    private int attempts;
  +
   
       public SequenceManagerHighLowImpl(PersistenceBroker broker)
       {
  @@ -222,34 +222,12 @@
                                           String sequenceName)
       {
           HighLowSequence newSequence = null;
  -        PersistenceBroker internBroker = null;
           try
           {
  -            Criteria c = new Criteria();
  -            c.addLike("tableName", sequenceName);
  -            if (useGlobalSequenceIdentities)
  -            {
  -                c.addLike("fieldName", PROPERTY_GLOBAL_SEQUENCE_ID);
  -            }
  -            else
  -            {
  -                c.addLike("fieldName", field.getColumnName());
  -            }
  -            Query q = new QueryByCriteria(HighLowSequence.class, c);
  +            Query q = buildQuery(sequenceName, field);
   
  -            /*
  -            arminw:
  -            we use a new internBroker instance, because we run into problems
  -            when current internBroker was rollback, then we have new sequence
  -            in memory, but not in database and a concurrent thread will
  -            get the same sequence.
  -            Thus we use a new internBroker instance (with new connection) to
  -            avoid this problem.
  -            */
  -            internBroker = PersistenceBrokerFactory.createPersistenceBroker(brokerForSequence.getPBKey());
  -            internBroker.beginTransaction();
               // first we lookup sequence object in database
  -            newSequence = (HighLowSequence) internBroker.getObjectByQuery(q);
  +            newSequence = (HighLowSequence) brokerForSequence.getObjectByQuery(q);
   
               //not in db --> we have to store a new sequence
               if (newSequence == null)
  @@ -267,8 +245,6 @@
               }
               //set current grab size
               newSequence.setGrabSize(grabSize);
  -            //use copy to avoid sync problems!!
  -            // newSequence = newSequence.getCopy();
   
               //grab the next key scope
               newSequence.grabNextKeySet();
  @@ -276,42 +252,57 @@
               //store the sequence to db
               try
               {
  -                /*
  -                arminw:
  -                remove object from cache to avoid problems when same
  -                objects where used with different databases. Currently
  -                (rc1) Identity does not differnce between databases, thus
  -                we could found an object in cache although it is not
  -                in the second database, it was used in the first database.
  -                If we not remove it PB try to update instead of insert.
  -                TODO: Find a better solution
  -                */
  -                internBroker.removeFromCache(newSequence);
  -                internBroker.store(newSequence);
  +                // brokerForSequence.store(newSequence);
  +                newSequence = storeSequence(brokerForSequence, newSequence);
               }
               catch (OptimisticLockException e)
               {
                   // we try five times to get a new sequence
  -                if(attempts < 5)
  +                if (attempts < 5)
                   {
  -                    log.info("OptimisticLockException was thrown, will try again to store
sequence. Sequence was "+newSequence);
  +                    log.info("OptimisticLockException was thrown, will try again to store
sequence. Sequence was " + newSequence);
                       attempts++;
                       getSequence(brokerForSequence, field, sequenceName);
                   }
  -                else throw e;
  +                else
  +                    throw e;
               }
  -            internBroker.commitTransaction();
  -
               if (log.isDebugEnabled()) log.debug("new sequence was " + newSequence);
           }
           finally
           {
               attempts = 0;
  -            if (internBroker != null) internBroker.close();
           }
           return newSequence;
       }
   
  +    private HighLowSequence storeSequence(
  +            PersistenceBroker brokerForSequence,
  +            HighLowSequence newSequence) throws OptimisticLockException
  +    {
  +        try
  +        {
  +            /*
  +            arminw:
  +            remove object from cache to avoid problems when same
  +            objects where used with different databases. Currently
  +            Identity does not difference between databases, thus
  +            e.g. we could found an object in cache although it is not
  +            in the second database, it was used in the first database.
  +            If we not remove it, PB try to update instead of insert.
  +            TODO: Find a better solution
  +            */
  +            brokerForSequence.removeFromCache(newSequence);
  +            brokerForSequence.store(newSequence);
  +        }
  +        catch (OptimisticLockException e)
  +        {
  +            throw e;
  +        }
  +
  +        return newSequence;
  +    }
  +
       private HighLowSequence newSequenceObject(String sequenceName,
                                                 FieldDescriptor field)
       {
  @@ -330,7 +321,7 @@
       }
   
       private long getMaxKeyForSequence(PersistenceBroker broker,
  -                                        FieldDescriptor field)
  +                                      FieldDescriptor field)
       {
           long maxKey = 0;
           if (useGlobalSequenceIdentities)
  @@ -345,5 +336,20 @@
               maxKey = SequenceManagerHelper.getMaxForExtent(broker, field);
           }
           return maxKey;
  +    }
  +
  +    private Query buildQuery(String sequenceName, FieldDescriptor field)
  +    {
  +        Criteria c = new Criteria();
  +        c.addLike("tableName", sequenceName);
  +        if (useGlobalSequenceIdentities)
  +        {
  +            c.addLike("fieldName", PROPERTY_GLOBAL_SEQUENCE_ID);
  +        }
  +        else
  +        {
  +            c.addLike("fieldName", field.getColumnName());
  +        }
  +        return new QueryByCriteria(HighLowSequence.class, c);
       }
   }
  
  
  
  1.5       +8 -4      db-ojb/src/java/org/apache/ojb/broker/util/sequence/HighLowSequence.java
  
  Index: HighLowSequence.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/sequence/HighLowSequence.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- HighLowSequence.java	1 Apr 2003 14:09:38 -0000	1.4
  +++ HighLowSequence.java	3 Sep 2003 18:45:29 -0000	1.5
  @@ -64,8 +64,6 @@
    * The HighLowSequence is the persistent part of the {@link SequenceManagerHighLowImpl}.
    * It makes the maximum reserved key persistently available.
    *
  - * @author    R Bischof
  - * @created   26. Oktober 2001
    * @version $Id$
    */
   public class HighLowSequence implements Serializable
  @@ -95,15 +93,21 @@
        */
       public HighLowSequence(String tableName, String fieldName, long maxKey, int grabSize)
       {
  +        this(tableName, fieldName, maxKey, grabSize, 0);
  +    }
  +
  +    public HighLowSequence(String tableName, String fieldName, long maxKey, int grabSize,
int version)
  +    {
           this.tableName = tableName;
           this.fieldName = fieldName;
           this.maxKey = maxKey;
           this.grabSize = grabSize;
  +        this.version = version;
       }
   
       public HighLowSequence getCopy()
       {
  -        return new HighLowSequence(this.tableName, this.fieldName, this.maxKey, this.grabSize);
  +        return new HighLowSequence(this.tableName, this.fieldName, this.maxKey, this.grabSize,
this.version);
       }
   
       public String toString()
  
  
  

---------------------------------------------------------------------
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