Can I Use Static fields in EJB ?


EJB programming & troubleshooting: Can I Use Static fields in EJB ?

  1. Can I Use Static fields in EJB ? (2 messages)

    I want to load data from database at regular time,and all instances of the EJB hold consistent data ,So I need a static object in EJB.But the ejb specification prohibit using static for read-write.Can i design a class using Singleton Pattern to load data from database and EJB access the class? Or what else method can I use for this problem?
  2. Can I Use Static fields in EJB ?[ Go to top ]

    If this is really important for your performance, you can rely on static fields with the following restriction in mind:
    Don't rely on any sharing semantics of static fields. Assume the static field may be un-initialized everywhere you use it. I.e, don't just load the information on start-up - make sure the information has been loaded wherever you use it.

    This restriction, while it is not formally "per spec", will probably work on every implementation. It is important not to interpret the spec too verbally. For instance, library code of things like XML Parsers, graphical frameworks, etc will allmost definately use read/write static fields. Singletons with lazy initialization are commonly used as factories and are one good example for this. EJB does not intend to deprecate all such libraries.
    Interpret the rule in the scope that it was given - don't rely on static fields as a way to share memory between beans. If your use of static fields will work even if there is no sharing (e.g, seperate VMs, clusters), it is acceptable. IMHO :)

  3. One can declare static fields in EJB but they must be final.