I would like JBoss to use only the dependencies located in my war file.
Each time I deploy this war file, JBoss still uses its own jars.
Here is the jboss-web.xml I use :
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<class-loading java2ClassLoadingCompliance="false">
<loader-repository>
my.package:loader=my-app.war
<loader-repository-config>
java2ParentDelegation=false
</loader-repository-config>
</loader-repository>
</class-loading>
</jboss-web>
and the jboss-classloading.xml :
<?xml version="1.0" encoding="UTF-8"?>
<classloading
xmlns="urn:jboss:classloading:1.0"
export-all="NON_EMPTY"
import-all="true"
parent-first="false"/>
JBoss 5.1.0.GA
1> SUMMARY
Initially, I have tried this class loading isolation for loading Hibernate 3.6.4 jars with JBoss 5.1.0.GA.
It’s definitively NOT possible. There is some magic under the hood that prevents you from using any Hibernate version with JPA2 support.
I’m really disappointed that JBoss project didn’t provide some kind of patch or service pack for supporting JPA2 on 5.1.0.GA.
2> WORKAROUND : “The Kernel solution”
I have managed to use JPA2 with JBoss 5.1.0.GA
I describe here my recipe. It’s more a proof of concept you can use to make your own solution.
Ingredients :
Recipe :
Step 1: Build the standalone application (APP)
This application will receive instructions from the servlet for using Hibernate.
I leave you the choice of the communication method.
As the APP uses JPA2, it will need a
persistence.xmlfile located in aMETA-INFfolder.Since JBoss 5.x, when you deploy a WAR, JBoss will scan the WAR and all its sub-deployments for finding and deploying blindly
persistence.xmlfiles. Rename yourpersistence.xmlfile intomy-persistence.xmlfor example. Use the code below when you build yourEntityManagerFactory(Prevent JBoss from deploying persistence.xml).UPDATE:
This method does work but some strange warnings are raised by Hibernate. In order to stop those warnings, I have decided to put the
META-INFfolder and the persistence file (renamed back topersistence.xmlnow) outside of the WAR. In my case, I choosed a special config folder on the hard drive and added it to the classpath. No more strange warnings and no custom classloader required for loading the persistence file.I leave it up to you to choose between using a custom class loader or changing the persistence file location. In both cases, JBoss won’t find the persistence file.
Step 2: Build the servlet
When the servlet needs to access the database, it launches the APP and tells it what to do.
For lauching the APP, the servlet is responsible of spawning a new JVM and build the classpath of the APP. Read the code below for (Spawning a JVM). The classpath is easily buildable since all the required jars will be in the
/libdirectory of the WAR archive…Step 3: Build the WAR archive
Build a WAR archive where you put the servlet and the standalone application packaged as a JAR. The APP will be a dependency of the WAR.
Prevent JBoss from deploying persistence.xml
Spawning a JVM