Re: Fetching record counts.
Re: Fetching record counts.
- Subject: Re: Fetching record counts.
- From: Arturo PĂ©rez <email@hidden>
- Date: Mon, 31 Jan 2005 22:57:56 -0500
On Jan 31, 2005, at 9:33 PM, Marek Wawrzyczny wrote:
This is actually where I am stuck. I don't understand the EOModel and
EOAttribute mechanism very well. One solution I tried was to add the
count attribute permanently, that crashed the application. So I tried
creating the attributes on the fly and removing them once finished
this worked. However, it doesn't appear I can make this thread-safe
since there is no locking mechanism on the EOModel (I think this is
why Eric's solution used a static var) and I cannot easily test this
concept under duress (when the exceptions actually start occurring).
Can anyone point me in the right direction on this?
EOF may not provide a locking mechanism but perhaps the Java one will
suffice? Have you tried synchronizing on the entity, something like:
public static Number objectCountWithFetchSpecification(
EOEditingContext editingContext,
EOFetchSpecification fetchSpecification) {
if (fetchSpecification != null) {
EOFetchSpecification rawFetchspecification;
try {
rawFetchspecification =
(EOFetchSpecification)fetchSpecification.clone();
} catch (Exception e) {
System.out.println("\nException in
objectCountWithFetchSpecification :\n"+e);
return null;
}
EOEntity entity =
EOModelGroup.defaultGroup().entityNamed(rawFetchspecification.entityName
());
synchronized (entity) { // ADDED SYNCHRONIZED
EOQualifier schemaBasedQualifier =
entity.schemaBasedQualifier(rawFetchspecification.qualifier());
EOAttribute attribute =
EOFetchCounting.objectCountAttribute();
NSArray results = null;
entity.addAttribute(attribute);
// Fix to ensure that fetch counting does not trip the site
when
// the fetch fails for some reason
try {
rawFetchspecification.setQualifier(schemaBasedQualifier);
rawFetchspecification.setRawRowKeyPaths(new
NSArray(attribute.name()));
results =
editingContext.objectsWithFetchSpecification(rawFetchspecification);
} finally {
// IMPORTANT!!!
// This needs to run every time, exception or not
entity.removeAttribute(attribute);
}
} // CLOSE SYNCHRONIZED
if ((results != null) && (results.count() == 1)) {
NSDictionary row = (NSDictionary) results.lastObject();
return (Number)row.objectForKey(attribute.name());
}
}
return null;
}
_______________________________________________
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