The OQLQueryImpl class contains a very 
subtle bug which can result in a sporadic 
NoSuchElementException or NullPointerException 
while iterating through a QueryResults object.  
I traced the problem and here's how it can be 
reproduced:

  Database database = jdo.getDatabase();
  database.begin();
  QueryResults queryResults =
database.getOQLQuery("SELECT foo FROM Foo
foo").execute();
  System.gc();  
  Runtime.getRuntime().runFinalization(); 
  queryResults.size());

Results in:
  java.lang.NullPointerException
        at
org.exolab.castor.jdo.engine.OQLQueryImpl$OQLEnumeration.size(OQLQueryImpl.java:567)
        ...

I believe this happens for the following reason: 
  1. The garbage collector runs and the OQLQueryImpl
object gets collected (since we are not holding on to
it).
  2. Finalizer runs calling OQLQueryImpl.finalize() 
  3. The finalize() method calls
OQLEnumeration.close() which nulls out the internal
_results variable.
  4. We query the size() method which attempts to
delegate to the internal _results variable but it's
null and so we get the exception.

Coding the query as follows makes the problem go away:

  Database database = jdo.getDatabase();
  database.begin();
  Query query = database.getOQLQuery("SELECT foo FROM
Foo foo"); // save the query to avoid garbage
collection
  QueryResults queryResults = query.execute();
  System.gc();  
  Runtime.getRuntime().runFinalization(); 
  queryResults.size());
        
-Oleg Barshay


__________________________________________________
Do You Yahoo!?
Yahoo! Sports - live college hoops coverage
http://sports.yahoo.com/

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to