Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB Stateless Initialization Pattern

I have an EJB Stateless Session Bean. I have these requirements:

  1. This Stateless EJB should be initialized at startup
  2. The initialization code should make a transactional access to a database

The problem is:

  1. @Startup is only available for @Singleton EJBs
  2. @PostConstruct annotation (at least on Websphere) doesn't have a transactional context at this point so the initialization code explode here!

Possible solutions?

  1. Use a Java EE Timer but it seems to be designed for periodic execution. I want just only one execution at time zero.
  2. Use a @Singleton + @Startup EJB just for initialization purposes and inject this singleton EJB to the dependant Stateless EJBs.

Question:

  1. Can any one explain how it is supposed a Stateless EJB to be initialized? or it has no sense? (I mean, stateless EJB are supposed to not to have initialization state?)
  2. Is there any pattern out there that says use of secondary EJB @Singleton with @Startup is a good idea?
like image 877
Andrés Oviedo Avatar asked Mar 11 '14 17:03

Andrés Oviedo


People also ask

What is stateless EJB?

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.

What is the difference between stateful and stateless EJB?

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.

Are stateless EJB thread safe?

Working with EJBs without any further configuration is thread-safe regardless whether you are invoking one method or multiple methods concurrently.

Which annotation is used for stateless EJB?

Annotations used in Stateless Session Bean @Stateless. @PostConstruct. @PreDestroy.


2 Answers

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.

like image 84
Andrés Oviedo Avatar answered Oct 02 '22 08:10

Andrés Oviedo


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?

like image 29
skay Avatar answered Oct 02 '22 07:10

skay