Multi-threaded access to DB in WOLongResponsePage [was Re: Problem with primary key generation]
Multi-threaded access to DB in WOLongResponsePage [was Re: Problem with primary key generation]
- Subject: Multi-threaded access to DB in WOLongResponsePage [was Re: Problem with primary key generation]
- From: Brendan Duddridge <email@hidden>
- Date: Thu, 26 Aug 2004 21:33:04 -0600
Hey all,
I'm cross-posting this to the Apple WebObjects development list because I
thought this would be interesting to some people there too.
> Aehm, I think it needs to be mentioned that the real "culprit" in this
> particular case was that two WO threads were using the same connection
> to the database which is a no-no.
>
> Regards,
> Geert B. Clemmensen
> www.frontbase.com
This is very true. And one often overlooked way a new thread is generated
that may need access to the database is when you create a WOLongResponse
page. You must create a new connection to the database in your
performAction() method. Do this by creating a new EOObjectStoreCoordinator.
The following code demonstrates this:
public Object performAction () {
EOObjectStoreCoordinator osc = new EOObjectStoreCoordinator();
EOEditingContext ec = new EOEditingContext(osc);
ec.lock();
while (some condition is true) {
if (this.isCancelled()) {
break;
}
... Do some processing to the database ...
}
CSEOUtilities.closeDatabaseChannelsForEditingContextAndModel(ec,
"ModelName");
ec.unlock();
ec.dispose();
ec = null;
osc = null;
return null;
}
The above code works nicely for me.
Oh, and here's the code for closeDatabaseChannelsForEditingContextAndModel:
// Given an editing context and a model name, close the channel to the
database. Only use this method if you've created your own
EOObjectStoreCoordinator for the specified editing context.
public static void
closeDatabaseChannelsForEditingContextAndModel(EOEditingContext ec, String
modelName) {
EODatabaseContext databaseContext =
EOUtilities.databaseContextForModelNamed(ec, modelName);
EOAdaptorContext adaptorContext = databaseContext.adaptorContext();
NSArray channels = adaptorContext.channels();
Enumeration channelEnum = channels.objectEnumerator();
while (channelEnum.hasMoreElements()) {
EOAdaptorChannel channel =
(EOAdaptorChannel)channelEnum.nextElement();
if (channel.isOpen()) {
channel.closeChannel();
}
}
}
Hope that helps!
___________________________
Brendan Duddridge
ClickSpace Interactive Inc.
Suite L100
239 - 10th Ave SE
Calgary, AB T2G 0V9
Canada
(403) 277-5591
http://www.clickspace.com/
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.