• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Prevent NEW in D2W?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Prevent NEW in D2W?


  • Subject: Re: Prevent NEW in D2W?
  • From: David Avendasora <email@hidden>
  • Date: Wed, 31 Aug 2011 19:33:00 +0800

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@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Prevent NEW in D2W?
      • From: Chuck Hill <email@hidden>
References: 
 >Prevent NEW in D2W? (From: Markus Ruggiero <email@hidden>)
 >Re: Prevent NEW in D2W? (From: Ramsey Gurley <email@hidden>)
 >Re: Prevent NEW in D2W? (From: Markus Ruggiero <email@hidden>)
 >Re: Prevent NEW in D2W? (From: David Avendasora <email@hidden>)
 >Re: Prevent NEW in D2W? (From: Q <email@hidden>)
 >Re: Prevent NEW in D2W? (From: Chuck Hill <email@hidden>)

  • Prev by Date: WOCollapsibleComponentContent broken?
  • Next by Date: Re: Prevent NEW in D2W?
  • Previous by thread: Re: Prevent NEW in D2W?
  • Next by thread: Re: Prevent NEW in D2W?
  • Index(es):
    • Date
    • Thread