Category: JPA, Websphere Gene De Lisa @ 9:45 am —
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...

Want to do something exotic like specifying two persistence units in your persistence.xml file? Perhaps one local and one JTA managed. Maybe even go whole hog and do that to write unit tests?

In Websphere v7 (RAD 7.5) when you have multiple PUs in persistence.xml it barfs like this:

[7/8/09 10:01:55:149 EDT] 00000053 servlet       I com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0242I: [someAppEAR] [/someAppJPAWeb] [/someAppUsersTable.jsp]: Initialization successful.
[7/8/09 10:01:55:227 EDT] 00000053 JPAApplInfo   E   CWWJP0012E: The persistence unit name is not specified and a unique persistence unit is not found in the someAppEAR application and someAppJPAWeb.war module.
[7/8/09 10:01:55:227 EDT] 00000053 JPAApplInfo   E   CWWJP0029E: The server cannot find the "" persistence unit in the someAppJPAWeb.war module and the someAppEAR application.
[7/8/09 10:01:55:227 EDT] 00000053 InjectionBind E   CWNEN0035E: The someAppJPAWeb reference of type javax.persistence.EntityManagerFactory for the <null> component in the someAppJPAWeb.war module of the someAppEAR application cannot be resolved.
[7/8/09 10:01:55:242 EDT] 00000053 annotation    E com.ibm.ws.webcontainer.annotation.WASAnnotationHelper inject exception while injecting resource

Here is IBM’s “help”:
http://publib.boulder.ibm.com/infocenter/dmndhelp/v6r2mx/index.jsp?topic=com.ibm.websphere.wbpm.messages.620.doc/messages/com.ibm.ws.jpa.jpa.html

CWWJP0012E: The persistence unit name is not specified and a unique persistence unit is not found in the {0} application and {1} module.
Problem    The application has defined a persistence unit or persistence context reference without a persistence unit name and a unique persistence unit cannot be found.
User response    Change the application to specify a persistence unit name for the reference, or provide a unique persistence.

Yeah, huh? What’s that mean? A reference in web.xml? and what name for it?
Can’t find the official answer anywhere. Let’s waste time searching and looking at logs and enjoying the wonderful WAS admin console.

OK, here is my answer.
Let’s say the JTA PU you want to use in your web app is named

<persistence-unit name="someAppJPAWeb"  transaction-type="JTA">
etc.

Then in web.xml create a PU reference:

<persistence-unit-ref>
<description>
Persistence unit for the someApp application.
</description>
<persistence-unit-ref-name>persistence/someAppJPAWeb</persistence-unit-ref-name>
<persistence-unit-name>someAppJPAWeb</persistence-unit-name>
</persistence-unit-ref>

In your injected class use the reference name:

@PersistenceUnit(name="persistence/someAppJPAWeb")
private EntityManagerFactory emf;

If you have just one PU then you don’t need the name attribute nor the persistence-unit-ref.

Of course when you are looking at persistence.xml RAD will helpfully tell you that it doesn’t like multiple persistence units.

Sorry, no comments yet.

Write Your Comment

Comment Guidelines: Basic XHTML is allowed (a href, strong, em, code). All line breaks and paragraphs will be generated automatically.

You should have a name, right? 
Your email address, I promised I won't tell it to anyone. 
If you have a web site or blog, you can type the URL right here. 
This is where you type your comments. 
Remember my information for the next time I visit.