On Aug 31, 2011, at 8:03 AM, Q wrote:
> Close, not Chuck. :)
Whoo hoo! I pulled Q _and_ Chuck into this. Sweeeet.
On Aug 31, 2011, at 8:03 AM, Q wrote:
>> public class MySingletonEO {
>>
>> private static MySingletonEO singleton = null;
>>
>> public MySingletonEO() {
>> // Don't call super() so this will stop instantiation.
>> // Not sure what EOF will do, probably die horribly,
>> // but really, that's okay, right?!
>> }
>
> Bug #1: This doesn't work. The java compiler always inserts an implicit
> super() call, if no such constructor exists in the parent the class will not
> compile. The only thing you can do to prevent instantiation is to throw an
> exception in the constructor or make the constructor private.
Er.. yeah. I was thinking protected, but typed public. Not sure how much of an
improvement that would have been, though.
>
>> public static MySingletonEO initialize(EOEditingContext editingContext) {
>> if(singleton == null) {
>> singleton = EOUtilities.faultWithPrimaryKeyValue(editingContext,
>>
>> MySingletonEO.ENTITY_NAME,
>>
>> Integer.valueOf(123456789)));
>> }
>> return singleton;
>> }
>> }
>
> Bug #2: This is a race condition. This method is not synchronised, so you
> could end up returning more than one "singleton" instance in a threaded
> environment.
Okay.
>> Bug #3: If this method is called more than once the returned EO may not have
>> been inserted into the provided editing context creating the potential for
>> changes to be lost if saveChanges is called on the wrong ec.
Holy crap. Yeah. That's what I get for writing something that I don't even try
to compile and run once. Bad, Dave. Bad. This is especially pathetic since I
just made the mistake of storing an EO in a static variable a couple weeks ago.
At least I figured that out on my own instead of publicly humiliating myself.
That would have been embarrassing. Whew.
> Yeah, a singleton EO makes no sense
Now _that's_ the response I was looking for. :-)
> as you need a separate instance in each EC (unless you want to use the Shared
> EC).
Well, that's kinda what the sharedEC is good for, no?
Except he said the user would be editing the values, so, no. That would be a
Bad Thing™ too.
> public static MySpecialEO instance(EOEditingContext editingContext) {
> return EOUtilities.faultWithPrimaryKeyValue(editingContext,
>
> MySingletonEO.ENTITY_NAME,
>
> Integer.valueOf(123456789)));
> }
>
> Is probably closer to what you want.
But none of this stops you from creating an second instance of the Entity, it
just makes it slightly easier to fetch the the right one.
I guess you could possibly do something at the DB level to keep the EO from
saving if created. Like a not-null-constrained column that EOF doesn't know
about... Then he'd have to catch the Adaptor exception.
Dave _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com
This email sent to [email protected]