Discussions

EJB programming & troubleshooting: static caches in SLSBs

  1. static caches in SLSBs (2 messages)

    I have a SLSB which returns data from a DB which is static between server start-ups. (lists of items for drop-down lists infact, although that isn't important)

    At the moment these are cached in a non-final static member of my SLSB EJB class.

    I know this is technically illegal according to the J2EE spec.

    My questions are:

    - when does doing it this way hurt?

    - what's the right way to do it?

    Thanks,

        Tom

    Threaded Messages (2)

  2. static caches in SLSBs[ Go to top ]

    I don't think this is actually technically illegal at all. I don't have my spec in front of me, but I do remember that it allows for the caching of data like this in the Stateless Session Beans. As long as the data is not specific to particular instances of the bean, there should be no problems with storing such data for the lifetime of the EJB...
  3. static caches in SLSBs[ Go to top ]

    The text from the 2.0 spec is: (C.1.2)

    "An enterprise Bean must not use read/write static fields. Using read-only static fields is allowed. Therefore, it is recommended that all static fields in the enterprise bean class be declared as final.

    This rule is required to ensure consistent runtime semantics because while some EJB Containers may use a single JVM to execute all enterprise bean’s instances, others may distribute the instances across multiple JVMs."

    So it seems to me that doing this is OK as long as I don't mind my cache being duplicated in seperate JVMs. (which I don't)

    Are there other problems?

    I would like to know what the 'sun-approved' way of caching data is.

    Tom