Re: Swap database instance at runtime
Mark Fisher <[email protected]> Tue, 17 Nov 2009 12:38:25 -0800
| Newsgroups | gmane.comp.web.webobjects.devel |
|---|---|
| Message-ID | <[email protected]> |
I use these methods in Application.java to change the DB connection
at runtime:
// ******************************
protected void setDBConnection(String serverURL){
if (null == serverURL)
serverURL = "jdbc:FrontBase://yourserver.com/
DatabaseName/user=_SYSTEM/isolation=read_committed/
locking=optimistic";//eq setDBConnection("yourserver.com");
else
serverURL = "jdbc:FrontBase://"+serverURL+"/" +
surveyTableName+ "/user=_SYSTEM/isolation=read_committed/
locking=optimistic";
//
String modelName = "EOModelName";
java.util.Enumeration e = EOModelGroup.defaultGroup
().models().objectEnumerator();
while (e.hasMoreElements() == true){
EOModel m = (EOModel)e.nextElement();
// use this part to isolate the model you wish
to connect
if (m.name().equals(modelName)){
NSMutableDictionary d = new
NSMutableDictionary(m.connectionDictionary());
//
d.setObjectForKey (serverURL,
com.webobjects.jdbcadaptor.JDBCAdaptor.URLKey);
//d.setObjectForKey (username,
JDBCAdaptor.UsernameKey);
//d.setObjectForKey (password,
JDBCAdaptor.PasswordKey);
//
m.setConnectionDictionary(d);
}
}
}
protected void setDBtable(String serverURL, String
tableName){
if (null == serverURL || null == tableName )
serverURL = "jdbc:FrontBase://yourserver.com/
DatabaseName/user=_SYSTEM/isolation=read_committed/
locking=optimistic"; //eq setDBConnection("yourserver.com");
else
serverURL = "jdbc:FrontBase://"+serverURL+"/" +
tableName+ "/user=_SYSTEM/isolation=read_committed/locking=optimistic";
//
String modelName = "EOModelName";
java.util.Enumeration e = EOModelGroup.defaultGroup
().models().objectEnumerator();
while (e.hasMoreElements() == true){
EOModel m = (EOModel)e.nextElement();
// use this part to isolate the model you wish
to connect
if (m.name().equals(modelName)){
NSMutableDictionary d = new
NSMutableDictionary(m.connectionDictionary());
//
d.setObjectForKey (serverURL,
com.webobjects.jdbcadaptor.JDBCAdaptor.URLKey);
//d.setObjectForKey (username,
JDBCAdaptor.UsernameKey);
//d.setObjectForKey (password,
JDBCAdaptor.PasswordKey);
//
m.setConnectionDictionary(d);
}
}
}
protected String getDBConnection(){
String serverURL=null;
String modelName = "EOModelName";
java.util.Enumeration e = EOModelGroup.defaultGroup
().models().objectEnumerator();
while (e.hasMoreElements() == true){
EOModel m = (EOModel)e.nextElement();
// use this part to isolate the model you wish
to connect
if (m.name().equals(modelName)){
NSMutableDictionary d = new
NSMutableDictionary(m.connectionDictionary());
serverURL = (String)d.objectForKey
(com.webobjects.jdbcadaptor.JDBCAdaptor.URLKey);
}
}
//NSLog.out.appendln("Application->getDBConnection() ="+serverURL);
return new String(serverURL);
}
// ******************************
Mark Fisher
Chief Technology Officer
LoTi Connection Inc.
On Nov 17, 2009, at 12:00 PM, [email protected]
wrote:
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Tue, 17 Nov 2009 10:46:03 +0100
> From: Rogier Bosch <[email protected]>
> Subject: Swap database instance at runtime
> To: [email protected]
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset=us-ascii; format=flowed; delsp=yes
>
> Hi All,
>
> I have one WO application which i would like to connect to different
> databases (1 db for each customer). The database schema is the same
> for all of them. My question is: how can i (if possible) @ runtime,
> swap the EO model/objects to connect to a different database instance?
>
> A small peace of example code would be appreciated.
>
> Many thanks in advance,
>
> Roger
_______________________________________________
WebObjects-dev mailing list
[email protected]
http://www.omnigroup.com/mailman/listinfo/webobjects-dev