I have an EJB Stateless Session Bean. I have these requirements:
The problem is:
Possible solutions?
Question:
A stateless session bean is a type of enterprise bean, which is normally used to perform independent operations. A stateless session bean as per its name does not have any associated client state, but it may preserve its instance state.
An instance of a stateful session bean has a unique identity that is assigned by the container at create time. Stateless: A stateless session bean does not maintain conversational state. Instances of a stateless session bean have no conversational state.
Working with EJBs without any further configuration is thread-safe regardless whether you are invoking one method or multiple methods concurrently.
Annotations used in Stateless Session Bean @Stateless. @PostConstruct. @PreDestroy.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With