Re: Different Database Connection per session for the same model
Re: Different Database Connection per session for the same model
- Subject: Re: Different Database Connection per session for the same model
- From: Lon Varscsak <email@hidden>
- Date: Mon, 18 Jun 2012 19:04:43 -0700
Here's how I implemented it (it's actually quite simple):
In Session.java:
public ERXObjectStoreCoordinator defaultObjectStoreCoordinator() {
return defaultObjectStoreCoordinator;
}
public void setDefaultObjectStoreCoordinator(ERXObjectStoreCoordinator defaultObjectStoreCoordinator) {
if (this.defaultObjectStoreCoordinator != null && this.defaultObjectStoreCoordinator != defaultObjectStoreCoordinator) {
this.defaultObjectStoreCoordinator.dispose();
this.defaultObjectStoreCoordinator = null;
System.gc();
}
this.defaultObjectStoreCoordinator = defaultObjectStoreCoordinator;
}
public static class OneConnectionPerSessionFactory extends ERXEC.DefaultFactory {
public OneConnectionPerSessionFactory() {
super();
}
@Override
public EOEditingContext _newEditingContext() {
return _newEditingContext(true);
}
@Override
public EOEditingContext _newEditingContext(boolean validationEnabled) {
EOObjectStore os = ((Session)ERXSession.session()).defaultObjectStoreCoordinator();
EOEditingContext ec = _newEditingContext(os, validationEnabled);
return ec;
}
}
@Override
public void terminate() {
setDefaultObjectStoreCoordinator(null); //this will cause a logout from the database
super.terminate();
}
In Application.java:
public Application() {
ERXApplication.log.info("Welcome to " + name() + " !");
ERXEC.setFactory(new Session.OneConnectionPerSessionFactory());
}
Basically, every where ERXEC.newEditingContext() is used it should use your new factory. I can't remember where I got this from…probably Mike…because I am not smart enough to figure this out (although I do get lucky every now and then :D). You should be able to use defaultEditingContext without worry too…although I rarely use it.
-Lon
_______________________________________________
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