Re: Database connection
Re: Database connection
- Subject: Re: Database connection
- From: Jean-François Veillette <email@hidden>
- Date: Wed, 8 Nov 2017 13:09:02 -0500
Hi André,
You are doing great learning WebObject by yourself! I encourage you to continue
and learn about WOnder too for many reasons.
While you continue learning on your own, you will find out that there is a
pitfall in setting connection dictionary like you did (once in the early
startup of the application).
Sometimes, the models can be reloaded (from the modeld file), so if you only
set it up once, then when they get reloaded the models won’t be set up
correctly (missing connection info).
So instead of setting up the model in the early application warmup, instead
register for a notification callback when models get loaded and change the
model when you get the notification.
There are many advantages of this:
- You can add new models and not care about changing the code to take care of
it (assuming it’s all connected to the same DB),
- This won’t force initialization of the model if they are not yet needed,
- This will take care of all models loaded and reloaded during application
lifetime.
You can continue to learn WebObjects on your own, falling in the not obvious
traps (like this one), or you can investigate WOnder where lots of people have
filled the gap and created ‘sane’ use patterns.
Have fun on the mailing list, it’s a long journey !
jfv
> On Nov 8, 2017, at 3:31 AM, André Rothe <email@hidden> wrote:
>
> Hi René,
>
> I have found a solution for my problems:
>
> In the JavaMonitor I have configured two applications with the same binaries.
> The only difference is a VM parameter -Dconfigfile=foo.config in the first
> application and the parameter -Dconfigfile=bar.config in the other
> application.
>
> With these parameters I can use different database configurations, which are
> stored into the configuration files.
>
> In the Application class I can use
>
> FileInputStream fs = new FileInputStream(System.getProperty("user.dir") +
> System.getProperty("file.separator") + System.getProperty("configfile"));
> Properties p = new Properties();
> p.load(fs);
>
> to get the database credentials. According to your post, in the model I have
> removed all connections, this will result in an Exception, if I access an
> EOEditingContext (i.e. within a Session). To prevent that, I set the
> connection within the Application class, before any access to the database.
>
> EOModel model = EOModelGroup.defaultGroup().modelNamed("MyModelName");
> EODatabaseContext dc =
> EODatabaseContext.registeredDatabaseContextForModel(model, new
> EOEditingContext());
> EOAdaptor da = dc.adaptorContext().adaptor();
>
> The database credentials I can store into an NSMutableDictionary:
>
> NSMutableDictionary cred = new NSMutableDictionary();
> cred.takeValueForKey(p.getProperty("username"), "username");
> cred.takeValueForKey(p.getProperty("password"), "password");
> cred.takeValueForKey(p.getProperty("url"), "URL");
> cred.takeValueForKey(p.getProperty("driver"), "driver");
>
> Then I set the new ConnectionDictionary for the EOAdapter and EOModel:
>
> da.setConnectionDictionary(cred);
> model.setConnectionDictionary(cred);
>
> This works also for all other EOEditingContext instances within the
> application, I don't have to switch the connection for every new EC instance.
>
> Some ideas I got from https://stackoverflow.com/questions/13459208 and
> http://mirror.informatimago.com/next/developer.apple.com/documentation/WebObjects/Enterprise_Objects/Connecting/chapter_10_section_8.html#//apple_ref/doc/uid/TP30001011-DontLinkChapterID_6-TPXREF144
>
> Best regards
> André
>
>
> Am 2017-11-06 17:05, schrieb René Bock:
>> Hi André
>> we don't store database connection infos in the eo-model. Instead we follow:
>> https://wiki.wocommunity.org/display/documentation/Best+Practices-Properties+Files
>> e.g:
>> dbConnectURLGLOBAL=jdbc:mysql://localhost/DbName
>> dbConnectUserGLOBAL=ojp
>> dbConnectPasswordGLOBAL=XXXXX
>> dbEOPrototypesEntityGLOBAL=EOJDBCMySQLPrototypes
>> dbConnectPluginGLOBAL=MySQLPlugIn
>>> Am 06.11.2017 um 16:55 schrieb André Rothe <email@hidden>:
>>> Hi,
>>> three questions according the database connection:
>>> I have a model, which has a database connection defined in a connection
>>> dictionary. But the model should not open the connection per default, it
>>> should get the credentials from another property file which will differ
>>> between the instances of the application in the JavaMonitor. If I use this
>>> solution:
>>> http://mirror.informatimago.com/next/developer.apple.com/documentation/WebObjects/Enterprise_Objects/Connecting/chapter_10_section_8.html
>>> the app will connect to the default database and makes then a reconnect to
>>> the later provided database. Is it possible to prevent the first connect?
>>> If I use multiple EOEditingContexts in my application, I have to change the
>>> connection parameters for every EC. When is the best time to set these for
>>> the session.defaultEditingContext()? Can I get the current connection
>>> credentials from an EditingContext instance to use it on another EC
>>> instance?
>>> Thank you.
>>> Best regards
>>> André
>>> _______________________________________________
>>> Do not post admin requests to the list. They will be ignored.
>>> Webobjects-dev mailing list (email@hidden)
>>> Help/Unsubscribe/Update your Subscription:
>>> This email sent to email@hidden
>> Mit freundlichen Grüßen
>> René Bock
>> --
>> Telefon: +49 69 650096 18
>> salient GmbH, Lindleystraße 12, 60314 Frankfurt
>> Telefon Zentrale: 069 / 65 00 96 - 0 | www.salient-doremus.de
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Webobjects-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden