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 Joss Wright <jo...@talk21.com> on 2003/09/06 21:26:09 UTC
Can the DB Alias be changed dynamically?
I am using OJB and HSQLDB in a web application. I would like to store my
HSQLDB files in the WEB-INF folder of the web application, without having to
define the whole path in the repository.xml file, just the database name.
I put OJB.properties in my WEB-INF/class folder so I use the class loader to
get the path to the OJB.properties file, then chop off the
class/OJB.properties part of the path to get the path to my WEB-INF
directory, e.g.:
C:/Documents and Settings/My Documents/My Workspace/My WebApp/WEB-INF/
I then want to add this to the default dbalias defined in the
repository.xml, e.g. file to give:
C:/Documents and Settings/My Documents/My Workspace/My
WebApp/WEB-INF/my-database
Then I'd like to create my PersistenceBroker with the dynamically defined
dbalias. The reason for this is that I can move my web application around
from server to server without having to redefine the path each time, just
the name.
I use the ServiceLocator pattern to return a PersistenceBroker and thought I
could do the following.
repository.xml:
<jdbc-connection-descriptor
jcd-alias="my-database"
default-connection="true"
platform="hsqldb"
jdbc-level="2.0"
driver="org.hsqldb.jdbcDriver"
protocol="jdbc"
subprotocol="hsqldb"
dbalias="my-database"
username="sa"
password="">
...
</jdbc-connection-descriptor>
ServiceLocator:
public PersistenceBroker getPersistenceBroker() throws
ServiceLocatorException {
PersistenceBroker broker = null;
try {
JdbcConnectionDescriptor descriptor = getDefaultDescriptor(); // see
getDefaultDescriptor() below
if (descriptor != null) {
// Get the path of OJB.properties
StringBuffer path = new StringBuffer(
descriptor.getClass().getClassLoader().getResource("OJB.properties").getPath
());
path = path.delete(path.indexOf("OJB.properties"), path.length());
String separator = path.substring(path.length()-1);
path = path.deleteCharAt(path.length()-1);
path = path.delete(path.lastIndexOf(separator)+1, path.length());
path = path.append(descriptor.getDbAlias());
descriptor.setDbAlias(path.toString());
}
broker = PersistenceBrokerFactory.createPersistenceBroker(new
PBKey(descriptor.getJcdAlias(),
descriptor.getUserName(), descriptor.getPassWord()));
return broker;
} catch (PBFactoryException e) {
throw new ServiceLocatorException(
"PBFactoryException occurred while getting default PeristenceBroker in
ServiceLocator.", e);
}
}
private JdbcConnectionDescriptor getDefaultDescriptor() {
List descriptors =
MetadataManager.getInstance().connectionRepository().getAllDescriptor();
for (Iterator i = descriptors.iterator(); i.hasNext(); ) {
JdbcConnectionDescriptor descriptor = (JdbcConnectionDescriptor)
i.next();
if (descriptor.isDefaultConnection()) {
MetadataManager.getInstance().connectionRepository().getDescriptor(descripto
r.getPBKey());
return descriptor;
}
}
return null;
}
However my call to descriptor.setDbalias(path) does not set the new dbalias.
Do I have to somehow save the JdbcConnectionDescriptor back to the
ConnectionRepository, or can I not intermix part pre-defined values with
dynamic values, i.e. do I have to create my JdbcConnectionDescriptor
completely dynamically.
Any help would be much appreciated,
Joss Wright
PS: I have studied all examples in the mail list and JavaDoc, already.
That's how I got this far!
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org