Finally i opted for:
- EJB
@Stateless
-- having a reference to --> EJB@Singleton
(with@Startup
)
That way, i can initialize the (shared and readonly) state or context necessary to serve requests.
سؤال
I have an EJB Stateless Session Bean. I have these requirements:
The problem is:
Possible solutions?
Question:
المحلول
Finally i opted for:
@Stateless
-- having a reference to --> EJB @Singleton
(with @Startup
)That way, i can initialize the (shared and readonly) state or context necessary to serve requests.
نصائح أخرى
Initializing a stateless EJB has no sense since this is the job of the Java EE container. Moreover, Java EE 6 provides the IOC pattern natively. IOC basically means hiding injected resources initialization process.
Your 2. solution is correct, as you need a transactional access.
Then you need to consider both cases/states :
a. singleton started up correcty
b. singleton failed at startup
In other words, are you sure that your (1.) statement is correct or you may interpret it with a lazy-init pattern ?
As the @startup occurs when application startup, maybe a state on the singleton with a lazy init activation is also matching to your needs?
The initialization code should make a transactional access to a database.
It is not clear to me for what the database access is, but in case you need fetch some data and store it as attributes of your stateless seasion bean, take in mind the following:
this information will be replicated in every stateless instance.
you will execute a database query every time a new bean instance is created.
at some point, it could be hard to be sure that all your beans instances have the same state.
I don't know the name of the pattern, but store the information in a Singleton session bean and inject it inside a stateless its a good solution. Even the Singleton bean can manage simultaneos requests, therefore, it won't become a bottleneck. It also allows you to manage possible information changes in a more consistent way.