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
Date Fri, 05 Sep 2003 22:06:59 GMT
arminw      2003/09/05 15:06:59

  Modified:    .        release-notes.txt
               xdocs    sequencemanager.xml
               src/java/org/apache/ojb/broker/util/sequence
                        SequenceManagerHighLowImpl.java
  Log:
  rollback to previous version, I can't
  get a grip on new version (deadlock problem)
  
  Revision  Changes    Path
  1.22      +0 -6      db-ojb/release-notes.txt
  
  Index: release-notes.txt
  ===================================================================
  RCS file: /home/cvs/db-ojb/release-notes.txt,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- release-notes.txt	3 Sep 2003 18:52:59 -0000	1.21
  +++ release-notes.txt	5 Sep 2003 22:06:59 -0000	1.22
  @@ -31,13 +31,7 @@
   to make additional proprietary methods available for user by
   casting org.odmg.Transaction to TransactionExt
   
  -- SequenceManagerHighLowImpl now can be used in managed environments
  -too
  -
   BUG FIXES:
  -
  -- Fixed. Under heavy multithreaded load SequenceManagerHighLowImpl
  -blocked application and induce PB-pool throw an timeout exception.
   
   Please refer to our Bug tracking site under
   http://scarab.werken.com/scarab/issues/id/OJBxxx to see details for a bug
  
  
  
  1.15      +5 -1      db-ojb/xdocs/sequencemanager.xml
  
  Index: sequencemanager.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/xdocs/sequencemanager.xml,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- sequencemanager.xml	3 Sep 2003 18:51:35 -0000	1.14
  +++ sequencemanager.xml	5 Sep 2003 22:06:59 -0000	1.15
  @@ -234,7 +234,11 @@
   </p>
   <p>
   Limitations:
  -<br/>- superfluously to mention, do not use if database will be updated by other
applications
  +<br/>- do not use in managed environments when connections were enlisted
  +in running transactions, e.g. when using DataSources of an application server
  +<br/>- if set connection-pool attribute 'whenExhaustedAction' to 'block' (wait for
  +connection), under heavy load the this SM could block application.
  +<br/>- superfluously to mention, do not use if other applications insert objects
   </p>
   
   <br/>
  
  
  
  1.22      +54 -61    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.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- SequenceManagerHighLowImpl.java	3 Sep 2003 18:45:29 -0000	1.21
  +++ SequenceManagerHighLowImpl.java	5 Sep 2003 22:06:59 -0000	1.22
  @@ -55,8 +55,9 @@
    */
   
   import org.apache.commons.lang.SystemUtils;
  -import org.apache.ojb.broker.OptimisticLockException;
   import org.apache.ojb.broker.PersistenceBroker;
  +import org.apache.ojb.broker.PersistenceBrokerFactory;
  +import org.apache.ojb.broker.OptimisticLockException;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
  @@ -120,9 +121,9 @@
    * <p>
    * <b>Limitations:</b>
    * <ul>
  - *	<li>Do NOT use this implementation in j2ee environment or
  + *	<li>Do NOT use this implementation in managed environment or
    * any comparable system where any connection was associated
  - * with the running tx.</li>
  + * with the running transaction.</li>
    * </ul>
    * </p>
    *
  @@ -150,13 +151,12 @@
       public static final String PROPERTY_GLOBAL_SEQUENCE_START = "globalSequenceStart";
   
   
  -    private static Map sequencesMap = new HashMap();
  -    private static int attempts;
  +    protected static Map sequencesMap = new HashMap();
   
       protected boolean useGlobalSequenceIdentities;
       protected int grabSize;
       protected long globalSequenceStart;
  -
  +    protected int attempts;
   
       public SequenceManagerHighLowImpl(PersistenceBroker broker)
       {
  @@ -212,22 +212,43 @@
                               SystemUtils.LINE_SEPARATOR + "PB: " + getBrokerForClass());
                   }
               }
  -
               return id;
           }
       }
   
  -    private HighLowSequence getSequence(PersistenceBroker brokerForSequence,
  +    protected HighLowSequence getSequence(PersistenceBroker brokerForSequence,
                                           FieldDescriptor field,
                                           String sequenceName)
       {
           HighLowSequence newSequence = null;
  +        PersistenceBroker internBroker = null;
           try
           {
  -            Query q = buildQuery(sequenceName, field);
  +            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);
   
  +            /*
  +            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) brokerForSequence.getObjectByQuery(q);
  +            newSequence = (HighLowSequence) internBroker.getObjectByQuery(q);
   
               //not in db --> we have to store a new sequence
               if (newSequence == null)
  @@ -245,6 +266,8 @@
               }
               //set current grab size
               newSequence.setGrabSize(grabSize);
  +            //use copy to avoid sync problems!!
  +            // newSequence = newSequence.getCopy();
   
               //grab the next key scope
               newSequence.grabNextKeySet();
  @@ -252,58 +275,43 @@
               //store the sequence to db
               try
               {
  -                // brokerForSequence.store(newSequence);
  -                newSequence = storeSequence(brokerForSequence, newSequence);
  +                /*
  +                arminw:
  +                remove object from cache to avoid problems when same
  +                objects where used with different databases. Currently
  +                Identity does not difference between databases, thus
  +                we can 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);
               }
               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,
  +    protected HighLowSequence newSequenceObject(String sequenceName,
                                                 FieldDescriptor field)
       {
           HighLowSequence seq = new HighLowSequence();
  @@ -320,8 +328,8 @@
           return seq;
       }
   
  -    private long getMaxKeyForSequence(PersistenceBroker broker,
  -                                      FieldDescriptor field)
  +    protected long getMaxKeyForSequence(PersistenceBroker broker,
  +                                        FieldDescriptor field)
       {
           long maxKey = 0;
           if (useGlobalSequenceIdentities)
  @@ -336,20 +344,5 @@
               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);
       }
   }
  
  
  

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