You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2003/03/14 02:44:20 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/odmg/oql OQLQueryImpl.java

arminw      2003/03/13 17:44:20

  Modified:    src/java/org/apache/ojb/odmg/collections DListImpl.java
               src/java/org/apache/ojb/odmg/oql OQLQueryImpl.java
  Log:
  fix j2ee integration, all tests
  passed now, do some
  improvements(hope so :)
  
  Revision  Changes    Path
  1.19      +53 -37    db-ojb/src/java/org/apache/ojb/odmg/collections/DListImpl.java
  
  Index: DListImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/collections/DListImpl.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- DListImpl.java	13 Mar 2003 19:57:47 -0000	1.18
  +++ DListImpl.java	14 Mar 2003 01:44:19 -0000	1.19
  @@ -54,10 +54,6 @@
    * <http://www.apache.org/>.
    */
   
  -import java.util.Iterator;
  -import java.util.ListIterator;
  -import java.util.Vector;
  -
   import org.apache.ojb.broker.ManageableCollection;
   import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.PBKey;
  @@ -82,6 +78,11 @@
   import org.odmg.OQLQuery;
   import org.odmg.Transaction;
   
  +import java.util.Vector;
  +import java.util.Iterator;
  +import java.util.ListIterator;
  +
  +
   
   /**
    * Insert the type's description here.
  @@ -93,9 +94,9 @@
   {
       private Logger log = LoggerFactory.getLogger(DListImpl.class);
       /**
  -	 * decide whether we can defer ID generation based on the type of persistentField we are using
  -	 * performance hit to generate these ID's is to much if you never use them.
  -	 */
  +     * decide whether we can defer ID generation based on the type of persistentField we are using
  +     * performance hit to generate these ID's is to much if you never use them.
  +     */
       private int id = -1;
       private Vector elements;
       private int size;
  @@ -113,7 +114,7 @@
           elements = new Vector();
           this.size = 0;
           if (!PersistentFieldFactory.usesAccessorsAndMutators())
  -			id = generateNewId();
  +            id = generateNewId();
       }
   
       /**
  @@ -126,7 +127,7 @@
           this.size = 0;
           this.pbKey = pbKey;
           if (!PersistentFieldFactory.usesAccessorsAndMutators())
  -			id = generateNewId();
  +            id = generateNewId();
       }
   
       /**
  @@ -336,10 +337,10 @@
        */
       public int getId()
       {
  -    	if (id == -1)
  -    	{
  -    		id = generateNewId();
  -    	}
  +        if (id == -1)
  +        {
  +            id = generateNewId();
  +        }
           return id;
       }
   
  @@ -449,36 +450,45 @@
           Criteria pCrit = pQ.getCriteria();
   
           PBCapsule handle = new PBCapsule(pbKey, TxManagerFactory.instance().getTransaction());
  -        PersistenceBroker broker = handle.getBroker();
  -
  -        Criteria allElementsCriteria = this.getPkCriteriaForAllElements(broker);
  -        // join selection of elements with predicate criteria:
  -        allElementsCriteria.addAndCriteria(pCrit);
  +        DList result;
   
  -        Class clazz = null;
           try
           {
  -            clazz = this.getElementsExtentClass(broker);
  -        }
  -        catch (PersistenceBrokerException e)
  -        {
  -            throw new ODMGRuntimeException(e.getMessage());
  -        }
  -        Query q = new QueryByCriteria(clazz, allElementsCriteria);
  -        if (log.isDebugEnabled()) log.debug(q.toString());
  +            PersistenceBroker broker = handle.getBroker();
   
  -        DList result = null;
  -        try
  -        {
  -            result = (DListImpl) broker.getCollectionByQuery(DListImpl.class, q);
  +            Criteria allElementsCriteria = this.getPkCriteriaForAllElements(broker);
  +            // join selection of elements with predicate criteria:
  +            allElementsCriteria.addAndCriteria(pCrit);
  +
  +            Class clazz = null;
  +            try
  +            {
  +                clazz = this.getElementsExtentClass(broker);
  +            }
  +            catch (PersistenceBrokerException e)
  +            {
  +                throw new ODMGRuntimeException(e.getMessage());
  +            }
  +            Query q = new QueryByCriteria(clazz, allElementsCriteria);
  +            if (log.isDebugEnabled()) log.debug(q.toString());
  +
  +            result = null;
  +            try
  +            {
  +                result = (DListImpl) broker.getCollectionByQuery(DListImpl.class, q);
  +            }
  +            catch (PersistenceBrokerException e)
  +            {
  +                log.error("Query failed", e);
  +                throw new OJBRuntimeException(e);
  +            }
           }
  -        catch (PersistenceBrokerException e)
  +        finally
           {
  -            log.error("Query failed", e);
  -            throw new OJBRuntimeException(e);
  +            // cleanup stuff
  +            if (handle != null) handle.destroy();
           }
  -        // cleanup stuff
  -        handle.destroy();
  +
           // 3. return resulting collection
           return result;
   
  @@ -540,7 +550,13 @@
        */
       public void ojbAddAll(org.apache.ojb.broker.ManageableCollection otherCollection)
       {
  -        this.addAll((DListImpl) otherCollection);
  +        // don't use this to avoid locking
  +        // this.addAll((DListImpl) otherCollection);
  +        Iterator it = otherCollection.ojbIterator();
  +        while (it.hasNext())
  +        {
  +            ojbAdd(it.next());
  +        }
       }
   
       /**
  
  
  
  1.13      +86 -80    db-ojb/src/java/org/apache/ojb/odmg/oql/OQLQueryImpl.java
  
  Index: OQLQueryImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/oql/OQLQueryImpl.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- OQLQueryImpl.java	9 Jan 2003 15:29:32 -0000	1.12
  +++ OQLQueryImpl.java	14 Mar 2003 01:44:20 -0000	1.13
  @@ -54,12 +54,8 @@
    * <http://www.apache.org/>.
    */
   
  -import java.io.StringReader;
  -import java.util.Enumeration;
  -import java.util.Iterator;
  -import java.util.ListIterator;
  -import java.util.Vector;
  -
  +import antlr.RecognitionException;
  +import antlr.TokenStreamException;
   import org.apache.ojb.broker.ManageableCollection;
   import org.apache.ojb.broker.PBKey;
   import org.apache.ojb.broker.PersistenceBroker;
  @@ -82,8 +78,11 @@
   import org.odmg.QueryInvalidException;
   import org.odmg.Transaction;
   
  -import antlr.RecognitionException;
  -import antlr.TokenStreamException;
  +import java.io.StringReader;
  +import java.util.Enumeration;
  +import java.util.Iterator;
  +import java.util.ListIterator;
  +import java.util.Vector;
   
   /**
    * Insert the type's description here.
  @@ -270,118 +269,125 @@
           //obtain current ODMG transaction
           Transaction tx = TxManagerFactory.instance().getTransaction();
           // create PBCapsule
  -        PBCapsule capsule = new PBCapsule(pbKey, tx);
  -        PersistenceBroker broker = capsule.getBroker();
  +        PBCapsule capsule = null;
  +        ManageableCollection result = null;
   
  -        // ask the broker to perfom the query.
  -        // the concrete result type is configurable
  -        ManageableCollection result;
  -
  -        if (!(query instanceof ReportQuery))
  -        {
  -            result = (ManageableCollection) broker.getCollectionByQuery(this.getCollectionClass(), query);
  -            performLockingIfRequired(tx, broker, result);
  -        }
  -        else
  +        try
           {
  -            Iterator iter = null;
  -            result = new ManageableArrayList();
  -            iter = broker.getReportQueryIteratorByQuery(query);
  -            try
  +            capsule = new PBCapsule(pbKey, tx);
  +            PersistenceBroker broker = capsule.getBroker();
  +
  +            // ask the broker to perfom the query.
  +            // the concrete result type is configurable
  +
  +            if (!(query instanceof ReportQuery))
  +            {
  +                result = (ManageableCollection) broker.getCollectionByQuery(this.getCollectionClass(), query);
  +                performLockingIfRequired(tx, broker, result);
  +            }
  +            else
               {
  -                while (iter.hasNext())
  +                Iterator iter = null;
  +                result = new ManageableArrayList();
  +                iter = broker.getReportQueryIteratorByQuery(query);
  +                try
                   {
  -                    Object[] res = (Object[]) iter.next();
  -
  -                    if (res.length == 1) 
  +                    while (iter.hasNext())
                       {
  -                        if (res[0] != null) // skip null values
  +                        Object[] res = (Object[]) iter.next();
  +
  +                        if (res.length == 1)
                           {
  -                            result.ojbAdd(res[0]);
  +                            if (res[0] != null) // skip null values
  +                            {
  +                                result.ojbAdd(res[0]);
  +                            }
                           }
  -                    }
  -                    else
  -                    {
  -                        // skip null tuples
  -                        for (int i = 0; i < res.length; i++)
  +                        else
                           {
  -                            if (res[i] != null)
  +                            // skip null tuples
  +                            for (int i = 0; i < res.length; i++)
                               {
  -                                result.ojbAdd(res);
  -                                break;
  +                                if (res[i] != null)
  +                                {
  +                                    result.ojbAdd(res);
  +                                    break;
  +                                }
                               }
                           }
                       }
                   }
  -            }
  -            finally
  -            {
  -                if (iter instanceof OJBIterator)
  +                finally
                   {
  -                    ((OJBIterator) iter).releaseDbResources();
  +                    if (iter instanceof OJBIterator)
  +                    {
  +                        ((OJBIterator) iter).releaseDbResources();
  +                    }
                   }
               }
  +            // reset iterator to start of list so we can reuse this query
  +            ListIterator it = getBindIterator();
  +            while (it.hasPrevious())
  +            {
  +                it.previous();
  +            }
  +        }
  +        finally
  +        {
  +            if (capsule != null) capsule.destroy();
           }
  -		// reset iterator to start of list so we can reuse this query
  -		ListIterator it = getBindIterator();
  -		while( it.hasPrevious()) 
  -		{
  -			it.previous();
  -		}
  -		
  -        capsule.destroy();
           return result;
       }
   
   
  -	protected void performLockingIfRequired(
  -		Transaction tx,
  -		PersistenceBroker broker,
  -		ManageableCollection result)
  -	{
  -		OdmgConfiguration config = getConfiguration();
  -		// if tx is available and implicit locking is required,
  -		// we do READ-lock all found objects
  -		if ((tx != null) && config.useImplicitLocking())
  -		{
  -		    // read-lock all resulting objects to the current transaction
  -		    Iterator iter = result.ojbIterator();
  -		    Object toBeLocked = null;
  +    protected void performLockingIfRequired(
  +            Transaction tx,
  +            PersistenceBroker broker,
  +            ManageableCollection result)
  +    {
  +        OdmgConfiguration config = getConfiguration();
  +        // if tx is available and implicit locking is required,
  +        // we do READ-lock all found objects
  +        if ((tx != null) && config.useImplicitLocking())
  +        {
  +            // read-lock all resulting objects to the current transaction
  +            Iterator iter = result.ojbIterator();
  +            Object toBeLocked = null;
               Class lastUsed = null;
               boolean didLockedLastUsed = false;
               Class current;
  -		    while (iter.hasNext())
  -		    {
  -		        toBeLocked = iter.next();
  +            while (iter.hasNext())
  +            {
  +                toBeLocked = iter.next();
                   current = toBeLocked.getClass();
  -		        /*
  +                /*
                   we can only lock objects, not attributes
                   arminw:
                   method #hasClassDescriptor is costly, so we try
                   to speed up lock-check by checking last used class first.
                   */
  -		        if ((current.equals(lastUsed) && didLockedLastUsed) ||
  +                if ((current.equals(lastUsed) && didLockedLastUsed) ||
                           broker.hasClassDescriptor(toBeLocked.getClass()))
  -		        {
  -		            tx.lock(toBeLocked, Transaction.READ);
  +                {
  +                    tx.lock(toBeLocked, Transaction.READ);
                       lastUsed = current;
                       didLockedLastUsed = true;
  -		        }
  +                }
                   else
                   {
                       lastUsed = current;
                       didLockedLastUsed = false;
                   }
  -		    }
  -		}
  -	}
  +            }
  +        }
  +    }
   
   
  -	protected OdmgConfiguration getConfiguration()
  -	{
  -		OdmgConfiguration config = (OdmgConfiguration) PersistenceBrokerFactory.getConfigurator().getConfigurationFor(null);
  -		return config;
  -	}
  +    protected OdmgConfiguration getConfiguration()
  +    {
  +        OdmgConfiguration config = (OdmgConfiguration) PersistenceBrokerFactory.getConfigurator().getConfigurationFor(null);
  +        return config;
  +    }
   
   
       /**