Re: Swap database instance at runtime

Chuck Hill <[email protected]> Wed, 18 Nov 2009 10:23:57 -0800
Newsgroups gmane.comp.web.webobjects.devel
Message-ID <[email protected]>
That is not a good way to set this information.  It only works if this  
happens before something else causes database activity.  Both  
Practical WebObjects and Project Wonder show a much better, fully  
reliable implementation.


Chuck



On Nov 18, 2009, at 4:07 AM, Riza Nugraha wrote:

> Hi all,
>
> I'm currently having the same situation here. I'm using more or less  
> the same method which is basically setting the new  
> connectionDictionary to my EOModel.
>
> I tried to fetch some records after switching the connection but  
> it's still giving me the result from the previous connection.
>
> Did I miss something? Should I use new editingContext every time I  
> switched to the new connection? I tried this as well .. but the  
> problem still persists. Kindly please advice.
>
> Best regards,
>
> Riza Nugraha
> ------------------
> Email: [email protected]
>
> 2009/11/17 Mark Fisher <[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: <28A4AAB2-D3AB-4F13-BDC4-8931C04DBAA4@future- 
>> engineers.nl>
>> 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
>
>
>
>
> _______________________________________________
> WebObjects-dev mailing list
> [email protected]
> http://www.omnigroup.com/mailman/listinfo/webobjects-dev

-- 
Chuck Hill             Senior Consultant / VP Development

Practical WebObjects - for developers who want to increase their  
overall knowledge of WebObjects or who are trying to solve specific  
problems.
http://www.global-village.net/products/practical_webobjects