Caching Data in EJB


EJB design: Caching Data in EJB

  1. Caching Data in EJB (10 messages)

    I am in need of way(s) to cache global type of data when we use EJB. (Global data that i have is read only).
    Like in servlets, we init global data in init() of servlet. Then everytime, henceforth, we use the loaded data. This we generally do my using a Singleton class that has static variables and methods/functions. (These variables generally are not declared as final).
    While using EJB, how do we do the same ? Like, if we have 10 beans and all want to access some global data that is loaded only once.
    We cant use the servlet solution in EJB because in EJB, the use of static variables are discouraged for required reasons.
    <Information That I Have>
    I have gone thru some documents that provide information saying we must bind the object in the JNDI tree. I have understood and have this 1 solution. Want to know if someone has used this and may be any other options are available.
    </Information That I Have>

    Threaded Messages (10)

  2. Caching Data in EJB[ Go to top ]

    Why using a Singleton with Static datamembers is discouraged ? Thats what we do and we do not have any problem !
    Then how about writing Serialized objects into a file and reading it during init ?
  3. Caching Data in EJB[ Go to top ]

    The singleton is implemented as a Session Bean?or just an ordinary java object?
  4. Caching Data in EJB[ Go to top ]

    Just as an ordinary java class

    - Dileep
  5. Caching Data in EJB[ Go to top ]

    If we deploy everything in the same JVM,then an ordinary java object works.However,if we deploy some components in a remote server,how can the remote objects access the singleton java object?
  6. Caching Data in EJB[ Go to top ]


    What I cud understand after going thru the document is :

    1) The static varibles will be initialised in each cluster. So its like replication of data.


    2) We can init it only once by registering it in JNDI tree and making it REPLICABLE false. Something like:

    <Copy from documentation>

    Who needs access to the object?
    Suppose your object only needs to be accessed by EJBs that are deployed on one server only. Obviously there would be no need to replicate this object throughout all of the servers in the cluster. In fact, you would specifically want to avoid doing so, to avoid any performance degradation due to unnecessary server-to-server cross-talk. In order to create a binding that is not replicated across servers, you must specify this option when creating the context that will be used to bind the object as shown in the example below:

      Hashtable ht = new Hashtable();
      //turn off binding replication
      ht.put(WLContext.REPLICATE_BINDINGS, "false");
      try {
        Context ctx = new InitialContext(ht);
        //bind the object
        ctx.bind("my_object", MyObect);
      } catch (NamingException ne) {
        //failure occured

    </Copy from documentation>

    Kindly refer to the link for more details as I myself I am new to all this.

  7. Caching Data in EJB[ Go to top ]

    How about Singleton with a lazy initialization ?
     - Ensure one instance of class for each current JVM.

    But you have to remember to run the application atleast once.
  8. Caching Data in EJB[ Go to top ]

    There's also a simple clustered singleton pattern using Coherence.


    Cameron Purdy
    Tangosol, Inc.
    Coherence: Easily share live data across a cluster!
  9. Caching Data in EJB[ Go to top ]

    Hi Srini,

    Well i went thru the J2EE specifications where its mentioned about NOT using static variables in J2EE framework because if the beans are deployed in a clustered environment then there will be issues. So in the J2EE specification it has been mentioned that if u declare anything as static, it must be also final.
    [ Anyways that is the case if the static variable value is modifiable which in my case is NOT going to be ].

    Also, What i had thought was in a clustered environment there will be multiple copies of data loaded in each cluster server. So how we cud achieve this by just loading the data once.
    [ I cud find a article for this : ]

    Thanks for your reply! It has helped be to clear the doubt.

  10. Caching Data in EJB[ Go to top ]


    If the information is un-changing, load it at startup into a singleton. Access the singleton from the EJB.

    If the data can change, it's only a problem in a clustered or farm environment (multiple JVMs and server machines). In that case, use Coherence.


    Cameron Purdy
    Tangosol, Inc.
    Coherence: Easily share live data across a cluster!
  11. Caching Data in EJB[ Go to top ]

    Ok Fine ! Understood !

    I will also go thru the "Coherence" link sent by you.

    Thanks for the information.

    Meanwhile, I cud find a document that might help all of us who are in this boat :) -