I'd thought I'd share a very nasty problem that occurs when OQLQuery and
QueryResults is used in a certain way - this has just cost me a fair amount
of time - and the correct usage (IMHO) is not clear from the documentation.

I have a method that returns a QueryResults instance (this is all fictitious
code, simplified to demonstrate the problem)

public QueryResults getResults()
{
        OQLQuery query = _db.getOQLQuery("select .....");
        return query.execute();
}

and a method that uses the above method:

public void foo()
{
        QueryResults results = getResults();

        //Iterate thru
        while (results.hasMore())
        {
                Widget w = (Widget)results.next();
        }
}

The problem with this code is that after the call to getResults(), the
OQLQuery instance goes out of scope - hence it is marked for garbage
collection.

Unfortunately the finalize() method of the OQLQuery class, closes its
associated resultset (!)

This means that if the OQLQuery instance is garbage collected before you've
finished iterating through the resultset, then suddenly in the middle of an
iteration, your code crashes with a mysterious null-pointer exception
(intermittently and in different places).

Moral of the story - always keep the OQLQuery and its resultset in the same
scope.

Moreover, always call close() explicitly on the resultset/query and don't
rely on garbage collection to close it. Putting code in finalizers is (IMHO)
rarely justified, and cause all sorts of weird bugs/deadlock etc.

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

Reply via email to