views:

12

answers:

1

Hi. Let's say I want to use an EJB container and I don't know how the container implementation is using classloaders.

Now, I want to have in-memory shared state with my custom datastructures (so no HSQLDB etc.). in this app., and the EJB spec happens to be < EJB 3.1 and I can't use the EJB singleton service.

If I'd use DI frameworks like Spring, Google Guice etc., will they be able to provide singleton injection? If so, how can they provide that?

+1  A: 

In practice it may (will probably) be possible, since most application servers are not particularly strict and allow you to "stretch" the J2EE specification a bit.

In theory, you cannot however guarantee a singleton within a J2EE application. One issue is that your application may be deployed across several VMs in a clustered environment, but even the EJB singleton (in EJB 3.1) will give you one singleton per VM. Another possible problem, even when running on one VM, is that your application may be suspended with state and later restarted in a different VM, e.g. after bouncing the server. In this case, the singleton pattern will be broken as well.

Following the J2EE strictly, you are also not allowed to use the synchronized statements or other thread locks in your code. This may make it impossible to guarantee integrity in a shared data structure. If you restrict your deployment to one VM, you should however not expect any other problems or issues, than if you were to run the same code in a non-JEE environment.

jarnbjo