Hi all,
I'm trying to use the ODMG API with multiple repositories/Databases.
Since OJB.java can only hold one open database per thread I always
have to call
((OJB) odmg).setCurrentDatabase (dbIWantToUseNext);
before getting a new Transaction with
tx = odmg.newTransaction ();
Is there any "best practice" for what I'm trying and/or is it ok the way
I do it at the moment ?
The testcase I've written (see attachment) works so far, but next step
is to get it working with multiple threads...
thanks for any response,
Jens
--
Jens Kr�mer
[EMAIL PROTECTED]
package org.apache.ojb.odmg;
import junit.framework.TestCase;
import org.odmg.*;
import org.apache.ojb.broker.PersistenceBrokerFactory;
import org.apache.ojb.broker.singlevm.PersistenceBrokerConfiguration;
import org.apache.ojb.broker.FarAwayClass;
import java.util.List;
/**
* Test to check support for multiple DB in the ODMG API.
*
* @author Jens Kraemer, [EMAIL PROTECTED]
*/
public class MultipleDBTest extends TestCase
{
private String secondRepository = "repositoryFarAway.xml";
private String repository;
protected static String NAME = "ODMG MultipleDBTest";
public MultipleDBTest(String s)
{
super(s);
}
protected void setUp() throws Exception
{
repository =
((PersistenceBrokerConfiguration) PersistenceBrokerFactory
.getConfigurator()
.getConfigurationFor(null))
.getRepositoryFilename();
}
protected void tearDown() throws Exception
{
super.tearDown();
}
/**
* tries to open two databases at the same time
*/
public void testOpenTwoDatabases () throws Exception
{
Implementation odmg = OJB.getInstance ();
DatabaseImpl db = (DatabaseImpl) odmg.newDatabase ();
DatabaseImpl dbFarAway = (DatabaseImpl) odmg.newDatabase ();
try {
db.open (repository, Database.OPEN_READ_WRITE);
assertTrue ("Database " + db + " could not be opened.", db.isOpen ());
System.out.println ("opened database " + db + " for repository " +
repository);
dbFarAway.open (secondRepository, Database.OPEN_READ_WRITE);
assertTrue ("Database " + dbFarAway + " could not be opened.",
dbFarAway.isOpen ());
System.out.println ("opened database " + dbFarAway + " for repository " +
secondRepository);
// check if db opened before is still open
assertTrue ("Database " + db + " not open any more.", db.isOpen ());
}
finally {
if (db != null) {
db.close ();
} // end of if (db != null)
if (dbFarAway != null) {
dbFarAway.close ();
} // end of if (db != null)
} // end of finally
}
public void testUseTwoDatabases () throws Exception
{
Implementation odmg = OJB.getInstance ();
DatabaseImpl db = (DatabaseImpl) odmg.newDatabase ();
DatabaseImpl dbFarAway = (DatabaseImpl) odmg.newDatabase ();
try {
dbFarAway.open (secondRepository, Database.OPEN_READ_WRITE);
db.open (repository, Database.OPEN_READ_WRITE);
((OJB) odmg).setCurrentDatabase (dbFarAway);
Transaction tx = odmg.newTransaction ();
tx.begin ();
FarAwayClass fac = getNewFarAwayClass ();
tx.lock (fac, tx.WRITE);
tx.commit ();
((OJB) odmg).setCurrentDatabase (db);
tx = odmg.newTransaction ();
tx.begin ();
Article article = getNewArticle ();
tx.lock (article, tx.WRITE);
tx.commit ();
((OJB) odmg).setCurrentDatabase (dbFarAway);
tx = odmg.newTransaction ();
tx.begin ();
OQLQuery q = odmg.newOQLQuery ();
q.create ("select x from " + FarAwayClass.class.getName () + " where
name=$1");
q.bind (NAME);
List results = (List) q.execute ();
assertEquals ("expected one instance of FarAwayClass:", 1, results.size ());
tx.commit ();
((OJB) odmg).setCurrentDatabase (db);
tx = odmg.newTransaction ();
tx.begin ();
q = odmg.newOQLQuery ();
q.create ("select x from " + Article.class.getName () + " where
articleName=$1");
q.bind (NAME);
results = (List) q.execute ();
assertEquals ("expected one instance of Article:", 1, results.size ());
tx.commit ();
}
finally {
if (db != null) {
db.close ();
} // end of if (db != null)
if (dbFarAway != null) {
dbFarAway.close ();
} // end of if (db != null)
} // end of finally
}
protected FarAwayClass getNewFarAwayClass ()
{
FarAwayClass retval = new FarAwayClass ();
retval.setName (NAME);
return retval;
}
protected Article getNewArticle ()
{
Article retval = new Article ();
retval.setArticleName (NAME);
return retval;
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>