You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by "David C. Hicks" <dh...@i-hicks.org> on 2003/06/13 22:01:54 UTC

Thanks Armin!

A couple of days ago you suggested to me a way to get multiple database 
connection descriptors to work in a single OJB environment, and I wanted 
to let you know that it has turned out successful.

For anyone else who might be interested, the procedure is something like 
this...

1) Load the repository descriptor information into MetadataManager.
2) Load the connection descriptor information into MM.  This connect 
descriptor needs to have a unique jcdAlias.
3) When asking for a PersistenceBroker be sure to use 
createPersistenceBroker() instead of defaultPersistenceBroker().

Here is a little sample code.  (jcdAlias is a String that is the name of 
the connection to the database.)

To load the new repository xml information:  (In our case, we decided on 
a convention to name the repository.xml files after the jcdAlias.)

        //    see if we need to merge new connections and descriptors 
into the metadata manager
        if (!jcdAlias.equals("default"))
        {
            //    make sure this connection hasn't already been loaded
            if 
(MetadataManager.getInstance().connectionRepository().getDescriptor(new 
PBKey(jcdAlias)) == null)
            {
                ConnectionRepository cr = 
MetadataManager.getInstance().readConnectionRepository(jcdAlias+"_repository.xml");
                MetadataManager.getInstance().mergeConnectionRepository(cr);
                DescriptorRepository dr = 
MetadataManager.getInstance().readDescriptorRepository(jcdAlias+"_repository.xml");
                MetadataManager.getInstance().mergeDescriptorRepository(dr);
            }
        }


To get a PersistenceBroker object:

        PBKey pbk = new PBKey(jcdAlias);
        PersistenceBroker broker = 
PersistenceBrokerFactory.createPersistenceBroker(pbk);

Armin also suggested that if you need to know which objects belong to 
which database, you can use an Attribute construct in each of your 
persistent objects:

       <Attribute attribute-name="datasource" attribute-value="myDatabase"/>

While we didn't need this because of our particular components, it's an 
easy change to make to each object descriptor and the information is 
readily available in MetadataManager after it gets loaded.

Thanks again, Armin!
Dave