File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Persistence Unit Scoping 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 "Persistence Unit Scoping" Watch "Persistence Unit Scoping" New topic

Persistence Unit Scoping

Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
Assume you have an EAR structure like this:

A persistence unit 'actionBazaar' is defined in both the actionBazaar-common.jar and actionBazaar-ejb.jar

If we try to use the 'actionBazaar' unit in the EJB module, the local persistence unit will be accessed. - I understand this.

However, if we try to use the the 'actionBazaar' unit in the Web module, which of the two persistence units will be used ?

My point here is, the web module has access to both these units, because the EAR class loader will load the actionBazaar-common.jar persistence unit and EJB class loader will load the actionBazaar-ejb.jar persistence unit.

Since, WAR Class loader, is the child of EJB class loader and EJB class loader is a child of EAR class loader, WAR module can access both the persistence units.

Hence which one will be invoked ?

Please correct me if I am making any mistake in understanding the scoping.
Yeray Santana Borges
Ranch Hand

Joined: Mar 02, 2008
Posts: 46
Hi Prasad

From the ejb-3.0-specification, point 6.2.2
A persistence unit that is defined at the level of an EJB-JAR,WAR,
or application client jar is scoped to that EJB-JAR, WAR, or application jar respectively and is visible to
the components defined in that jar or war. A persistence unit that is defined at the level of the EAR is
generally visible to all components in the application.

If you try to use a persistence unit from actionBazaar-web.jar, you are getting the persistence unit from lib/actionBazaar-common.jar, because in war file you have not a persistence unit. The web module only has access to ear level persistence unit.

You must follow this rule and forget the class loader in persistence unit scopes. I hope it has helped you

I agree. Here's the link:
subject: Persistence Unit Scoping
jQuery in Action, 3rd edition