Can InitialContex be a Singleton ?

Discussions

EJB programming & troubleshooting: Can InitialContex be a Singleton ?

  1. Can InitialContex be a Singleton ? (5 messages)

    Hi all,
    is it possible to keep the InitialContext as static instance of a Singleton, or it's not correct?

    That is:

     try {
      javax.naming.Context ctx = SingletonContext.getInstance();

      javax.sql.DataSource ds
      = (javax.sql.DataSource) ctx.lookup (s);

      conn = ds.getConnection();

      }

    Hope I was clear
    Thanks
    Francesco
  2. InitialContext can be a singletonon the client side, meaning a client like a servlet or java application, but mot inside an EJB.

    To reduce number of instantiations in an ejb set a transient instance variable for Context in the
    set[Session | Entity]Context()


    public class MyEJB implements SessionBean
    {

      private transient Context initCtx;

      private SessionContext ctx;

      public void setSessionContext(SessionContext ctx) {
         
         ctx = _ctx;
         
         // any code set up this instance
         initCtx = new InitialContext();
         // you can also cache you DataSource and resources lookups

      }
    ....

    }

    Also the reason you don't want to have a singleton inside ejb is user identity associated with Context.

    Hope this helps.
  3. very clear.
    Thanks a lot
    Francesco
  4. I share the following code between all ejb's and it seems to work fine. (jboss 2.x and 3.x)

    public class ContextUtils {

      private static InitialContext initialContext=null;

      public static InitialContext getInitialContext() throws NamingException {

        if(initialContext==null)
          initialContext = new InitialContext();
        return initialContext;

      }

      ...other helper methods

    }

    public class EjbUtils {

      public static Object getHomeByJndiName(String jndiName, Class type) throws NamingException {
        return getHomeObject(jndiName, type, false);
      }

      public static Object getHomeByCompName(String compName, Class type) throws NamingException {
        return getHomeObject(compName, type, false);
      }

      public static Object getLocalHomeByJndiName(String jndiName, Class type) throws NamingException {
        return getHomeObject(jndiName, type, true);
      }

      public static Object getLocalHomeByCompName(String compName, Class type) throws NamingException {
        return getHomeObject(compName, type, true);
      }

      protected static Object getHomeObject(String name, Class type, boolean local) throws NamingException {
        Object href = ContextUtils.getInitialContext().lookup(name);
        return (local)?href:PortableRemoteObject.narrow(href, type);
      }

      ...other helper methods
    }

    @Michael Malkinzon
    Could you please explain, why not to use singletons on the server-side (in a ejb-container)?

    Regards,
    Michael (Germany)
  5. Can InitialContex be a Singleton ?[ Go to top ]

    Why not?

    Well - because it violates spec:

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

    And why does the spec contain this restriction?

    One hint is given by the spec itself: "... to ensure consistent runtime sematics ... " across servers which might use one or multiple JVM instances for all bean instances.

    In your case you add one issue as mentioned above: EJB Security does not work as expected. You will have one security principal per classloader only (so to say per deployed ejb-jar in most cases), the security identity used while initializing your util class.

    Hope this helps.

    Jens
  6. Can InitialContex be a Singleton ?[ Go to top ]

    Just a minor addition:

    Although the specs talks about static fields of EJBs your util class behaves not that much different.