1 2 Previous Next 16 Replies Latest reply on Feb 9, 2012 10:17 PM by Oleg Stepanov Go to original post
      • 16. Re: @Singleton @PostConstruct, multiple invocations
        Oleg Stepanov Newbie

        Ok, fix for https://issues.jboss.org/browse/AS7-2785 has solved the problem when one singleton refers to another via @EJB injection.

        However, I seem to be facing the same issue when EJB bean obtains singleton reference not by injection but via an explicit JNDI lookup.

         

        Attached is a stripped down example with the following structure:

        ear:

           ejb1:

              @Startup @Singleton A1

              @Startup @Singleton B1

          ejb2:

              @Startup @Singleton A2

              @Startup @Singleton B2

         

        Singletons A2 and B2 make JNDI lookups in their @PostConstruct methods, each one for both A1 and B1. After that they call testMethod() on both A1 an B1 (in different order). NOTE: I deliberately do not use @DependsOn annotation on either A2 or B2 since in my real application these are not singletons but SLSB and I observe the same results.

         

        Now, when I deploy this ear to JBoss 7 (I use post-7.1.0.CR1 snapshot build #513), sometimes I get well expected result:

        05:39:33,634 INFO  [org.test.B1] (MSC service thread 1-3) starting, instance id 2545222

        05:39:33,637 INFO  [org.test.A1] (MSC service thread 1-1) starting, instance id 3928552

        05:39:34,635 INFO  [org.test.B1] (MSC service thread 1-3) started, instance id 2545222

        05:39:34,638 INFO  [org.test.A1] (MSC service thread 1-1) started, instance id 3928552

        05:39:34,643 INFO  [org.test.B1] (MSC service thread 1-2) testMethod, instance id 2545222

        05:39:34,645 INFO  [org.test.A1] (MSC service thread 1-2) testMethod, instance id 3928552

        05:39:34,647 INFO  [org.test.A1] (MSC service thread 1-4) testMethod, instance id 3928552

        05:39:34,648 INFO  [org.test.B1] (MSC service thread 1-4) testMethod, instance id 2545222

         

        Here a single instance of A1 and B1 is created then testMethod is called twice on each instance (once by A2 and once by B2)

         

        However, other times I get something like this:

        05:44:11,901 INFO  [org.test.B1] (MSC service thread 1-1) starting, instance id 15920931

        05:44:11,917 INFO  [org.test.A1] (MSC service thread 1-2) starting, instance id 473254

        05:44:12,901 INFO  [org.test.B1] (MSC service thread 1-1) started, instance id 15920931

        05:44:12,907 INFO  [org.test.B1] (MSC service thread 1-3) testMethod, instance id 15920931

        05:44:12,917 INFO  [org.test.A1] (MSC service thread 1-2) started, instance id 473254

        05:44:12,918 INFO  [org.test.A1] (MSC service thread 1-4) starting, instance id 6373940

        05:44:13,919 INFO  [org.test.A1] (MSC service thread 1-4) started, instance id 6373940

        05:44:13,920 INFO  [org.test.A1] (MSC service thread 1-4) testMethod, instance id 473254

        05:44:13,921 INFO  [org.test.A1] (MSC service thread 1-3) testMethod, instance id 473254

        05:44:13,921 INFO  [org.test.B1] (MSC service thread 1-4) testMethod, instance id 15920931

         

        As you can see, here the second instance of A1 is created (but only the first one is used in calls).

         

        Restarting JBoss several times I even got this:

        05:41:55,358 INFO  [org.test.B1] (MSC service thread 1-4) starting, instance id 14035661

        05:41:55,360 INFO  [org.test.A1] (MSC service thread 1-3) starting, instance id 32784682

        05:41:56,359 INFO  [org.test.B1] (MSC service thread 1-4) started, instance id 14035661

        05:41:56,361 INFO  [org.test.A1] (MSC service thread 1-3) started, instance id 32784682

        05:41:56,360 INFO  [org.test.B1] (MSC service thread 1-2) starting, instance id 12922195

        05:41:56,362 INFO  [org.test.A1] (MSC service thread 1-1) starting, instance id 25694010

        05:41:57,362 INFO  [org.test.B1] (MSC service thread 1-2) started, instance id 12922195

        05:41:57,364 INFO  [org.test.A1] (MSC service thread 1-1) started, instance id 25694010

        05:41:57,368 INFO  [org.test.B1] (MSC service thread 1-2) testMethod, instance id 14035661

        05:41:57,368 INFO  [org.test.A1] (MSC service thread 1-1) testMethod, instance id 32784682

        05:41:57,369 INFO  [org.test.B1] (MSC service thread 1-1) testMethod, instance id 14035661

        05:41:57,369 INFO  [org.test.A1] (MSC service thread 1-2) testMethod, instance id 32784682

         

        Here both A1 and B1 PostConstruct methods are called twice (and yet again, only the first instances are used in calls).

         

        Although the fact that only one instance is used in subsequent calls makes problem smaller, multiple calls of PostConstruct methods are still not acceptable for obvious reasons.

        Any ideas?

        1 2 Previous Next