This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
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

Win a copy of Building Microservices this week in the Design forum!
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: 2007

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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: @PostConstruct confusion
jQuery in Action, 3rd edition