For a project I need to have a unique ID generator. So I thought about a Singleton with synchronized methods.
Since a Singleton following the traditional Singleton pattern (private static instance
) is shared accross Sessions, I'm wondering if the @Singleton
Annotation is working the same way?
The documentation says: Identifies a type that the injector only instantiates once.
Does it mean, that a @Singleton
will be independent per User Session
(which is bad for an id-generator)? Should I prefer an old school
Singleton with Class.getInstance()
over an Injection of an @Singleton
-Bean?
Or should I use neither nor and provide the Service within an @ApplicationScoped
bean?
it musst be guaranteed that only ONE thread, independent of the user session can access the method to generate the next id. (It's not solvable with auto-increment database ids)
Edit: JSF 2.2, CDI and javax.inject.*
i'm talking about :)
All those kinds of singletons (static, @javax.inject.Singleton, @javax.ejb.Singleton and @javax.enterprise.context.ApplicationScoped) are created once per JVM.
You can technically have multiple applications running in a single JVM, and the spec allows for it. These items are generally scoped to the application, making ApplicationScoped the best named – Inquietude