[Xapian-discuss] Xapian JNI questions

James Aylett james-xapian at tartarus.org
Thu Dec 7 09:35:11 GMT 2006


On Thu, Dec 07, 2006 at 02:47:14AM +0000, Olly Betts wrote:

> So *if* Java doesn't reliably call the C++ destructor, you should use
> flint instead of quartz (see http://wiki.xapian.org/FlintBackend for
> details of how to do this) and make sure you call flush() on each
> WritableDatabase before you exit.
> 
> I say "*if*" because I don't know Java and JNI well enough to know
> what the situation is here.  I vaguelly recall that Eric Ridge (who
> wrote the JNI bindings originally) once said something about
> problems with finalisers getting called, but I can't find the mail.
> I think he said there's a Java API call which addresses this
> problem, but that the Sun documentation says it's buggy and should
> be avoided or something like that.  Without the mail it's hard to be
> sure I'm not misremembering though.

In general, you shouldn't use Java destructors to free non-simple
resources like database locks, because you can't control when the
destructor is called. Under certain circumstances, the destructor
isn't called at all unless you're careful (and under some JVMs, there
are bugs which mean they won't necessarily get called ever, period).

The common practice is to have a close() method which closes the
database, and have the destructor call that (then make close() safe
for multiple calls). Something like:

----------------------------------------------------------------------
class XapianDatabaseWrapper {
    private Xapian::Database *db;
    public void close() {
        del *this.db;
	*this.db = null;
    }
}    
----------------------------------------------------------------------

(which is probably slightly wrong - been a while since I thought in
C++ :-).

J

-- 
/--------------------------------------------------------------------------\
  James Aylett                                                  xapian.org
  james at tartarus.org                               uncertaintydivision.org



More information about the Xapian-discuss mailing list