I believe there are cases when it's not just as easy as just taking the persistence unit out of the persistence.xml file. For instance, if you want Seam or Spring to bootstrap the persistence unit instead of the Java EE container, you are in a bit of a fix, especially if you plan to use the Java SE standard way of loading the persistence unit (you can obviously go around it like Spring does and load a file with a different name). Also, what you gain by convention (loading all persistence units in persistence.xml) you lose in build complexity. For instance, Hibernate Tools allows you to have multiple persistence units defined in persistence.xml which you can use for testing queries, but then you have to expunge those in your build logic before deploying. Definitely worth doing for a production build, but not something to worry about early on in development.
All I'm saying is that I can anticipate plenty of scenarios where I will need to disable this automatic loading of persistence units (or selective loading) and I don't know how to do that in JBoss AS 5. I'm willing to say that the default behavior is what we want, but I strongly believe that control over this mechanism needs to be documented.
According to Ales, these are the current ways to control this behavior:
By order of severity:
1) remove or replace deployer that adds this behavior
- writing new MC deployers is trivial
2) add new deployer that alters this behavior
- MC deployers are metadata driven chain
3) add new structure deployer
- e.g. custom Seam structure
4) limit where persistence.xml is valid (see jboss-structure.xml)
5) rename files: persistence.xml --> persistence-xyz.xml
If this kicks in via @PersistenceContext:
1) limit scanning (as I already posted in JIRA):
- https://www.jboss.org/community/docs/DOC-13178 (see jboss-scanning.xml)
To see this problem in action in a Seam example, follow these steps:
svn co http://anonsvn.jboss.org/repos/seam/trunk
set jboss.home in build.properties to point to an JBoss AS 5 installation
watch for failed deployment in the log files:
java.lang.IllegalArgumentException: Can't find a persistence unit
named 'null' in
Adding empty jboss-scanning.xml results in this:
21:23:57,312 ERROR [[/jboss-seam-spring]] Exception sending context initialized event to listener instance of class org.jboss.seam.servlet.SeamListener java.lang.IllegalStateException: Two components with the same name and precedence - component name: org.jboss.seam.ui.entityLoader, component classes: org.jboss.seam.ui.JpaEntityLoader, org.jboss.seam.ui.converter.entityConverter.EntityLoader at org.jboss.seam.init.Initialization.addComponentDescriptor(Initialization.java:596) at org.jboss.seam.init.Initialization.installScannedComponentAndRoles(Initialization.java:950) at org.jboss.seam.init.Initialization.scanForComponents(Initialization.java:890) at org.jboss.seam.init.Initialization.init(Initialization.java:701) at org.jboss.seam.servlet.SeamListener.contextInitialized(SeamListener.java:34) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3910)
It appears that you have dead files remaining in your source tree that you used to build Seam (or you have different versions of Seam on the classpath). You can see that the second component was removed a couple months ago in a package restructuring.