On Nov 13, 2007, at 3:02 PM, Zak Burke wrote:
> Mike Schrag wrote on 11/13/07 4:30 PM:
>>> I can only assume his java files are doing something funky in a
>>> static block or something?
>> Oh right .. looking at that stack trace for 5 seconds:
>> at edu.dartmouth.dltg.pns.eos.SourceDnd.sourceDndById
>> (SourceDnd.java:45)
>> at edu.dartmouth.dltg.pns.eos.SourceDnd.<clinit>(SourceDnd.java:20)
>> You're doing EOF stuff in a static initializer. This is just a
>> bad idea. You should really do this kind of initialization after
>> your app loads
>
> Ah; that's good to know. ... How should I have known? This is the part
> of WO programming that sometimes drives me nuts: there's some Java
> pattern that is normal
Fetching from a database in a static initializer is a normal Java
pattern? Uh, you sure about that?
> (this is all derived from a standard way of
> managing typesafe enums prior to JDK 1.5) but that causes WO to
> explode
> and Chuck, Anjo and Mike say, "Oh, yeah, that. Don't do that." How do
> you know? How did you learn? Or is it just obvious if you truly
> understand EOF what kinds of behaviour will lead to trouble?
If you understand how bundles and EOF initializes and how the JVM
handles static initializers, then is reasonably obvious.
> Before I get carried away on my rant, is the basic answer that I
> have to
> replace this:
>
> A.
> public class SourceDnd extends _SourceDnd {
>
> public static final SourceDnd DARTMOUTH_DND =
> SourceDnd.sourceDndById(Integer.valueOf(1));
> ...
> }
>
> with this:
>
> B.
> public class OtherClassNotRelatedToAnEo {
>
> private static final SourceDnd DARTMOUTH_DND = null;
> private static final SourceDnd ALUMNI_DND = null;
>
> public static final DARTMOUTH_DND() {
> if (OtherClass.DARTMOUTH_DND == null)
> OtherClass.DARTMOUTH_DND = SourceDnd.sourceDndById(
> Integer.valueOf(1));
That won't compile as DARTMOUTH_DND is declared as final. You will
have to forgo the final.
>
> return OtherClass.DARTMOUTH_DND;
> }
> ...
> }
You do have a race condition there that can result in multiple
fetches, but I would not consider that a serious problem. Call that
method in your Application constructor / when the app finishes
launching to avoid that.
> or do I have to give up on the efficiency of static variables all
> together and implement these as methods only:
>
> C.
> public class OtherClassNotRelatedToAnEo {
>
> public static final DARTMOUTH_DND() {
> return SourceDnd.sourceDndById(Integer.valueOf(1));
> }
> ...
> }
Static is fine. Accessing EOF in static initializers is rather dodgy.
Chuck
--Practical WebObjects - for developers who want to increase their overall knowledge of WebObjects or who are trying to solve specific problems. http://www.global-village.net/products/practical_webobjects
This archive was generated by hypermail 2.0.0 : Tue Nov 13 2007 - 18:25:41 EST