EJB programming & troubleshooting: Can I cache the home object in servlet?

  1. Can I cache the home object in servlet? (2 messages)

    Is it possible to cache a copy of home object as static object in servlet that can be used by multiple threads to create ejb by calling home.create()??
  2. Can I cache the home object in servlet?[ Go to top ]


    Using a static variable for storage of the home object will give a situation where the code is unclear (unless you write synchronised code). You will also be in areas where the specification is a bit "open" for interpretation.

    But I have another solution: Get the home object in the init() method and store it in an instance variable. (I am not 110% sure the init() method has access the the InitialContext, so you have to check this!) The container will create one or a few Servlet instances, so this will be "good enough" when it somes to performance.

    From the instance variable, you can call home.create(...). Also note that you normally cannot share EJB objects (unless the Servlet implements SingleThreadModel).

  3. Can I cache the home object in servlet?[ Go to top ]

    Now that I understand why to make home object an instance variable rather than class variable.
    Since I need the InitialContext to lookup for home object and the InitialContext is quite 'static', can I make it static final in my servlet?
    If that's not possible for some reason, I can still access the InitialContext in init() method right before i create the home object right?