Hi Sean,

Sean Dockery wrote:

Two questions...

1) Getting connections from a PersistenceBroker...

In the documentation, it says that you can acquire a connection object
directly from the PB API.  By the fact that you can also acquire brokers
from an Implementation instance, can I assume that the following is legal?

Implementation odmg = OJB.getInstance();
Transaction tx = odmg.getTransaction();
tx.beginTransaction();
PersistenceBroker broker = ((HasBroker)  tx).getBroker();
Connection connection = broker.serviceConnectionManager().getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
//...
tx.commitTransaction();

The docs says to NOT close a connection... but what about Statement and
ResultSet resources?


The Statement and ResultSet are straightly opened by yourself, so you have to close these resources by yourself.


2) Using PersistenceBroker.getIteratorByQuery...

Is the iterator returned by PersistenceBroker.getIteratorByQuery valid
after the persistence broker has been closed (either explicitly because I'm
using the PB API or implicitly by closing the an ODMG transaction.  That
is, is this valid...

Implementation odmg = OJB.getInstance();
Transaction tx = odmg.getTransaction();
tx.beginTransaction();
PersistenceBroker broker = ((HasBroker) tx).getBroker();
Criteria criteria = new Criteria();
criteria.addEqualTo("category.id", category.getId());
criteria.addOrderByAscending("name");
Query query = QueryFactory.newQuery(Product.class, criteria);
Iterator iterator = broker.getIteratoryByQuery(query);
tx.commitTransaction();

while(iterator.hasNext()) {
    Product product = (Product) iterator.next();
    // blah blah blah
}

Is it valid to use the iterator after the transaction has been committed
(i.e.: broker has been closed)?


This will (should ;-)) cause an exception, because the DB resources used by the iterator will be closed at tx.commit.
So the answer is no.
After the PB instance was closed, the instance itself (the PB handle) will become invalid, all further method calls should cause an exception (except PB.close(), PB.isClosed(), PB.isInTransaction())


regards,
Armin

Thanks for your time...





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



Reply via email to