Meaningless Drivel is fun!
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes @PostConstruct confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "@PostConstruct confusion" Watch "@PostConstruct confusion" New topic

@PostConstruct confusion

Shankar sanjay
Ranch Hand

Joined: Sep 14, 2009
Posts: 117

I am using EJB stateless session bean, due to some reason,we want to INITIALIZE database connection or database resource lookup(datasource) in @PostContruct method.

My understanding is that the ejb container will decide to create new EJB Bean whenever number of calls from clients goes high (correct me if i am wrong), now doubt is that each instance will invoke @PostContruct method? or only the very first bean invoke @PostContruct method.

if each bean instance will invoke @PostContrct method then we will be looking datasource (jndi look) redundantly.
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper

Joined: Apr 07, 2010
Posts: 2099

Hi Shankar,

now doubt is that each instance will invoke @PostContruct method?

Yes, the PostConstruct method will run for every instance.

In EJB 3.1 you could use the @Singleton EJB (with an @Startup annotation)

I agree. Here's the link:
subject: @PostConstruct confusion
It's not a secret anymore!