Category

Multiple Persistence Units – Websphere v7, OpenJPA

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:

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

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

Then in web.xml create a PU reference:

In your injected class use the reference name:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.