As has been suggested, an RsIterator is no longer valid once its underlying 
broker has been closed.

I can't find the code at the moment, so maybe I'm dreaming, but I'm pretty sure 
I wrote a BrokerClosingIterator once, which could be used like so:

Iterator iter = null;
try {
  broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  broker.beginTransaction();
  iter = BrokerClosingIterator.wrap(broker.getIteratorByQuery(query), broker);
  broker.commitTransaction();
} catch (PersistenceBrokerException t) {
  broker.abortTransaction();
  broker.close();
  t.printStackTrace();
}

while (iter.hasNext()) {
  Person person = (Person) iter.next();
  ....
  some code
  ....
}

BrokerClosingIterator would do a broker.close() in two places: 1) when next() 
consumes the last item; and 2) in a finalizer, in case we don't ever consume the 
full contents of the Iterator.  A problem is that it's not possible to guarantee 
that the broker is closed, since the finalizer might never run.  Shouldn't be 
too hard to add a timeout, though - it seems that most RsIterators shouldn't 
need to live very long?

-steve

Steve Clark
Technology Applications Team
Natural Resources Research Center/USGS
[EMAIL PROTECTED]
(970)226-9291


>List-Id: "OJB Users List" <ojb-user.db.apache.org>
>Subject: problem/question with getIteratorByQuery()
>Date: Thu, 30 Oct 2003 16:00:29 +0100
>Thread-Topic: problem with PB instances creation
>From: <[EMAIL PROTECTED]>
>To: <[EMAIL PROTECTED]>
>
>Hello,
>
>I have a question/problem about the function getIteratorByQuery():
>
>Here is a code:
>
>****code****
>Iterator iter = null;
>try {
>  broker = PersistenceBrokerFactory.defaultPersistenceBroker();
>  broker.beginTransaction();
>  iter = broker.getIteratorByQuery(query);
>  broker.commitTransaction();
>} catch (PersistenceBrokerException t) {
>  broker.abortTransaction();
>  t.printStackTrace();
>} finally {
>  broker.close();
>}
>
>while (iter.hasNext()) {
>  Person person = (Person) iter.next();
>  ....
>  some code
>  ....
>}
>****code****
>
>I have noticed that the Iterator object called iter isn't available after the 
broker is closed (no iteration in the "while" statement).
>Why?
>
>My idea is to use a broker only when I need one and close it just after the 
use.
>What is the solution?
>But could I write the code as below?
>
>****code****
>try {
>  broker = PersistenceBrokerFactory.defaultPersistenceBroker();
>  broker.beginTransaction();
>  Iterator iter = broker.getIteratorByQuery(query);
>  while (iter.hasNext()) {
>    Person person = (Person) iter.next();
>    ....
>    some code
>    ....
>  }
>  broker.commitTransaction();
>} catch (PersistenceBrokerException t) {
>  broker.abortTransaction();
>  t.printStackTrace();
>} finally {
>  broker.close();
>}
>****code****
>
>Thanks for any help
>Sylvain
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to