This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Persistence Unit Scoping" Watch "Persistence Unit Scoping" New topic
Author

Persistence Unit Scoping

Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
Assume you have an EAR structure like this:
lib/actionBazaar-common.jar
actionBazaar-ejb.jar
actionBazaar-web.jar

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


SCJP 6 - SCWCD 5 - SCBCD 5
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Persistence Unit Scoping