Hi Thomas,
----- Original Message -----
From: "ojb ojb" <[EMAIL PROTECTED]>
To: "OJB Users List" <[EMAIL PROTECTED]>
Sent: Monday, November 18, 2002 7:34 PM
Subject: User-Defined Key Generator
> Hi everybody,
>
> I'm just playing around with a user-defined key generator. I'm having
a query, and since there is a value "0" (zero) for some key, the OJB key
generator starts to trigger (well, actually this is not nice, but it
helps me testing my generator anyhow).
>
> So, here's the question:
> (1) during my query, I see several of these:
>
> [org.apache.ojb.broker.ta.PersistenceBrokerFactoryDefaultImpl] INFO:
Already created persistence broker instances: 1
>
> [org.apache.ojb.broker.ta.PersistenceBrokerFactoryDefaultImpl] INFO:
Already created persistence broker instances: 2
>
> [org.apache.ojb.broker.ta.PersistenceBrokerFactoryDefaultImpl] INFO:
Already created persistence broker instances: 3
>
> Why? Can't he use just one PB instance? I'm using the ODMG interfaces,
just creating one implementation, one database, one transaction and one
query...
PB are the central class in the OJB framework, when other classes need a
PB
they grab an PB instance from the pool, thus you don't see only one
instance.
Each PB has it's own sequence manager instance, instantiated in
constructor.
What are the reasons for implementing your sequence manager?
regards,
Armin
>
> implementation = OJB.getInstance();
> database = implementation.newDatabase();
> try
> {
> database.open(configuration, Database.OPEN_READ_WRITE);
> } catch (ODMGException ex)
> {
> ...
> }
> ... tx open ...
> impl.newTransaction()
> EnhancedOQLQuery query = db.getImplementation().newOQLQuery();
> query.create("select p from a.b.c.DEF");
> DList result = (DList) query.execute();
>
> (2) Inside my Key Generator, I am always getting a different PBroker
than the one that read the query. This causes a second database to be
opened. Why/can I configure to use the same connection?
Yes
>
> Here's the generator:
>
> import org.apache.ojb.broker.util.sequence.SequenceManager;
> import org.apache.ojb.broker.util.configuration.Configurable;
> import org.apache.ojb.broker.util.configuration.Configuration;
> import org.apache.ojb.broker.PersistenceBroker;
> import java.sql.Statement;
> import org.apache.ojb.broker.metadata.ClassDescriptor;
> import org.apache.ojb.broker.query.Query;
> import java.sql.ResultSet;
> import java.sql.SQLException;
> import java.util.logging.Level;
> import java.util.logging.Logger;
> import org.apache.ojb.broker.util.sequence.SequenceConfiguration;
> import org.apache.ojb.broker.PBKey;
> import org.apache.ojb.broker.PersistenceBrokerFactory;
>
> public class OJBHiLoSequenceImpl implements SequenceManager,
Configurable {
>
> private static final String STMT_SELECT_1 = "SELECT MAX(";
> private static final String STMT_SELECT_2 = ") FROM ";
>
> protected static Logger logger = Logger.getLogger("plexx.testsuite");
>
> private PBKey pbKey;
> private PersistenceBroker brokerForClass;
>
> public OJBHiLoSequenceImpl(PersistenceBroker broker)
> {
> logger.log(Level.FINEST, "<init> with broker " + broker);
> this.brokerForClass = broker;
> }
>
> public int getUniqueId(Class par1, String par2) {
> ClassDescriptor cld = brokerForClass.getClassDescriptor(par1);
> // [TP] 2002-11-18 TODO care about multiple table mappings and
inheritance mappings
> String tableName = cld.getFullTableName();
> String fieldName = cld.getFieldDescriptorByName(par2).getColumnName();
> Statement stmt = null;
> ResultSet set = null;
> try
> {
> PersistenceBroker brokerForSeq = null;
> // [TP] 2002-11-14 if we have a so-called extra broker key, we should
> // [TP] 2002-11-14 use this instance to ask for a
connection (key
> // [TP] 2002-11-14 passed during config)
> if (pbKey == null)
> {
> brokerForSeq =
PersistenceBrokerFactory.defaultPersistenceBroker();
> } else
> {
> brokerForSeq =
PersistenceBrokerFactory.createPersistenceBroker(pbKey);
> }
> logger.log(Level.FINEST, "getUniqueId: using broker " +
brokerForSeq);
> stmt = brokerForSeq.getStatementManager().getGenericStatement(cld,
Query.NOT_SCROLLABLE);
> set = stmt.executeQuery( STMT_SELECT_1 + fieldName +
STMT_SELECT_2 + tableName);
> if (set.next())
> {
> return set.getInt(1);
> } else
> {
> throw new SQLException("Empty result set for table " +
tableName + " and field " + fieldName);
> }
> } catch (SQLException ex)
> {
> // [TP] 2002-11-18 TODO EX
> throw new RuntimeException(ex);
> }
> }
>
> public long getUniqueLong(Class par1, String par2){
> return new Long(getUniqueId(par1, par2)).longValue();
> }
>
> public String getUniqueString(Class par1, String par2){
> return Integer.toString(getUniqueId(par1, par2));
> }
>
> public Object getUniqueObject(Class par1, String par2){
> return new Integer(getUniqueId(par1, par2));
> }
>
> public void configure(Configuration par1){
> SequenceConfiguration conf = (SequenceConfiguration) par1;
> this.pbKey = conf.getSeparatePBKey();
> }
> }
>
>
>
>
> I know this is a lot of code, but if anybody has a glue - I would be
happy.
>
> Thanks for all,
>
> Thomas
>
>
> --
> To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>
>
>
>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>