3 Replies Latest reply: Aug 29, 2013 5:40 AM by gimbal2 RSS

    EJB and Singleton Pattern

    user10400926

      I am trying to bring a legacy jar file into an EJB.  The legacy jar file manages a Java Singleton using the ThreadLocal pattern -- that is each thread appears to be able to use a POJO Singleton because the real state is held in a ThreadLocal.

       

      Now I want to use this jar in an EJB and it is failing.  It appears the WebLogic container is switching threads in the middle of a session, throwing the ThreadLocal off.

       

      Is there some equivalent pattern in EJB's that I can use?  Is there some statically accessible context data where I can store some information?

       

      advTHANKSance

       

      Chas Douglass

        • 1. Re: EJB and Singleton Pattern
          gimbal2

          user10400926 wrote:

           

          I am trying to bring a legacy jar file into an EJB.  The legacy jar file manages a Java Singleton using the ThreadLocal pattern -- that is each thread appears to be able to use a POJO Singleton because the real state is held in a ThreadLocal.

           

          Now I want to use this jar in an EJB and it is failing.  It appears the WebLogic container is switching threads in the middle of a session, throwing the ThreadLocal off.

           

          Is there some equivalent pattern in EJB's that I can use?  Is there some statically accessible context data where I can store some information?

           

          advTHANKSance

           

          Chas Douglass

          What do you mean by "session" exactly? Its a pretty non-specific term that can already translate to several different things in the context of a JEE environment.

           

          You could use something like EHCache as an alternative to ThreadLocal, which has a pretty easy programming interface.

          • 2. Re: EJB and Singleton Pattern
            user10400926

            Thanks for replying.

             

            I'm sure a big part of the problem here is that I am a Java Applications programmer and not at all familiar with JEE programming.  In my mind a "session" should be from the time the user identifies/authenticates themselves until they logout or timeout.

             

            Chas Douglass

            • 3. Re: EJB and Singleton Pattern
              gimbal2

              The closest you can get in an EJB (which does not care about sessions) is a stateful EJB. That's a concept that is not easy to grasp and I must say that documentation on the proper usage of a stateful EJB is quite lacking.