I am new to OJB, therefore my questions about it could be very naive...
please be patient :)
My questions concern cache cleaning in a persinstance broker. Please
consider the following example: I have implemented a simple class, Test. Its
main removes all the records from the associated table and inserts a new
one; all these operations are performed within a transaction.

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerException;
import org.apache.ojb.broker.PersistenceBrokerFactory;
import org.apache.ojb.broker.query.Query;
import org.apache.ojb.broker.query.QueryByCriteria;

public class Test implements Serializable {
    public static void main(String[] argv) {

        Collection myCollection = null;
        Test toInsert = null;
        try {

            // get an instance of
org.apache.ojb.broker.core.PersistenceBrokerImpl
            PersistenceBroker broker =
PersistenceBrokerFactory.defaultPersistenceBroker();

            broker.beginTransaction();
            System.out.println("start");

            Query qry = new QueryByCriteria(Test.class, null);
            myCollection = broker.getCollectionByQuery(qry);
            System.out.println("collection selected");

            Iterator iter = myCollection.iterator();
            while (iter.hasNext()) {
                broker.delete((Test)iter.next());
                System.out.println("delete");
            }

            toInsert = new Test();
            toInsert.setDescription("new");
            toInsert.setCurrTimeStamp("2004-02-29");

            broker.store(toInsert);
            System.out.println("insert");
            broker.commitTransaction();
            broker.close();
            System.out.println("commit");
        } catch (Exception e) {
            e.printStackTrace(System.err);
            Iterator iter = myCollection.iterator();
            while (iter.hasNext()) {
                try {
                    // remove every object from cache
                    broker.removeFromCache((Test)iter.next());
                } catch (PersistenceBrokerException pbe) {
                    // empty catch: ignoring errors
                }
            }
            try {
                broker.removeFromCache(toInsert);
            } catch (PersistenceBrokerException pbe) {
                // empty catch: ignoring errors
            }
            broker.abortTransaction();
        }
    }

    public int getId(){ return id; }
    public void setId(int id){ this.id = id; }

    public String getDescription(){ return description; }
    public void setDescription(String description){ this.description =
description; }

    public String getCurrTimeStamp(){ return currTimeStamp; }
    public void setCurrTimeStamp(String currTimeStamp){ this.currTimeStamp =
currTimeStamp; }

    private int id;
    private String description;
    private String currTimeStamp;
}

About this example, possible problems came in my mind about cache cleaning
in case of errors: some of the objects involved indeed may contain wrong
data if the transaction fails and it seem reasonable to remove them from the
broker cache.

1.
First, I'd like to know whether the cache has to be cleaned or not - I mean,
in a large project must I clean the cache using removeFromCache (or
something else?) when a transaction fails? ...or calling abortTransaction is
enough?

2.
Is the position of the statement abortTransaction() correct? (after the
cache clean)

3.
A colleague of mine noticed that if an object is not in the cache, this
exception is thrown:
org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException: Could
not init Identity for given object class xyz
This is a RuntimeException! Have we to catch it leaving the catch body empty
(as I did in the example above in the case of PersistenceBrokerException)?

Thanks in advance,
Marcello Zaniboni


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

Reply via email to