How to put a Persistent Plain Old Java Object (POJO) into JNDI
 Technote (troubleshooting)
 
Problem(Abstract)
A common question is how to place a persistent Java™ object (POJO) or primitive into JNDI which all applications in a cell can access, share, and modify.
 
Resolving the problem
As described in the following Information Center article, the key to putting a persistent POJO into JNDI in a J2EE™-compatible way is to bind to the "cell/persistent/" context (see PersistentJndiHelper.bindPersistentJndiObject):
Lookup names support in deployment descriptors and thin clients

In the attached sample Enterprise application, there is a GlobalPOJOEar project which contains the GlobalPojoWeb project. The WAR contains a servlet called GlobalPojoServlet which registers the persistent object in it's init() method. The servlet has <load-on-startup>-1</load-on-startup> set in the deployment descriptor so that the servlet is initialized on deployment and registers the persistent POJO.

Finally, there is a GlobalPOJOTestEar project which contains the GlobalPojoTest Web project. That project has an index.jsp which retrieves the persistent POJO from JNDI and displays the value.

Notes:
  • The class being bound should implement java.io.Serializable (see SimplePojo.java in the PersistentJndi project).
  • The PersistentJndiHelper class is a simple wrapper around the JNDI calls necessary to put an object into cell-wide scope. The basic idea is:

    To add the object to the cell-wide scope:

    InitialContext context = new InitialContext();
    context.bind("cell/persistent/myobj", new MyObj());

    To lookup the object from any application in the cell:

    InitialContext context = new InitialContext();
    return (MyObj)context.lookup("cell/persistent/myobj");

    With the only caveat that class MyObj implements java.io.Serializable.

PersistentPojoInJndi.zip
  • In IBM® WebSphere® Application Server V6.0, the Resource Environment allows the creation and administration of scope-wide Resource Environment Providers, which let you do functionally the same thing as above in a declarative way. In Application Server V6.1, adding primitives like Strings and Integers becomes even easier in a declarative manner. Under the covers, WebSphere Application Server uses basically the same approach as above, utilizing the persistent JNDI scope.
 
Related information
Lookup names support in deployment descriptors and thin
 
 
 


Document Information


Product categories: Software > Application Servers > Distributed Application & Web Servers > WebSphere Application Server > JNDI/Naming
Operating system(s): z/OS
Software version: 6.1
Software edition:
Reference #: 1268860
IBM Group: Software Group
Modified date: Aug 29, 2007