2 Replies Latest reply on Nov 22, 2012 3:32 AM by Tim Troup

    Performance impact of injecting javax.ejb.Singleton vs javax.inject.Singleton

    Tim Troup Newbie

      I am building a Java EE 6 web application running on JBoss AS 7.1.1 final. In the app we have defined a Singleton EJB and injected that in to another class. (I know that javax.ejb.Singleton is not part of the CDI spec but please bear with me.)

       

      We noticed a performance issue in our code whereby method invokations on the injected object took 60ms to complete where we saw them as being near instantaneous when run against a non-injected instance.

       

      When we changed the injected class to be  a javax.inject.Singleton we saw a massive speed up in our code. Back to near instantaneous execution.

       

      Can anyone tell me why method execution on an injected javax.ejb.Singleton is so badly affected?

        • 1. Re: Performance impact of injecting javax.ejb.Singleton vs javax.inject.Singleton
          atomicknight Newbie

          By default, singleton session beans are transactional (section 13.3.7 of the EJB 3.1 specification) and require acquisition of an exclusive lock for every business method invocation (sections 4.8.5.4 and 4.8.5.5). In contrast, a javax.inject.Singleton is not transactional and does not support container-managed concurrency (the major consequence being that no locking scheme is implemented by the container).

           

          If you annotate your singleton session bean with @TransactionAttribute(NOT_SUPPORTED) and @Lock(READ), you should see significantly better performance, though there may still be some overhead. If you don't need EJB features, stick with @ApplicationScoped (javax.inject.Singleton is not defined by CDI, and its semantics are therefore not governed by that specification).

          • 2. Re: Performance impact of injecting javax.ejb.Singleton vs javax.inject.Singleton
            Tim Troup Newbie

            Thanks for the info. I had already annotated the singleton session bean with @Lock(READ) but not @TransactionAttribute(NOT_SUPPORTED). Since I don't need EJB features I will stick to @ApplicationScoped.