I am using jboss 5.1.0 GA and am connecting to a postgresql DB from my application using hibernate. I am using the default hibernate jars found within jboss. Within my application war I am only packaging application specific jars and other 3rd party jars NOT found within jboss.
If I place my postgres jdbc jar under server/xxxx/lib it gets picked.
If I place it under web-inf/lib of my application war file it doesn't get picked and I get -
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:545)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at org.hiber开发者_C百科nate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
My question is how do I get jboss to load the postgres driver jar from under web-inf/lib?
Like I said, the hibenate jars are that from jboss and I am not packaging them with my app.
Update: Another question. In which scenarios are we expected to use the default hibernate jars and in what possible scenarios would we want to package the hibernate jars along with our application?
Thanks,
My question is how do I get jboss to load the postgres driver jar from under web-inf/lib?
To my knowledge, this is not possible. To put it simply, class loaders are arranged in a parent-child tree. When a class loader is asked to load a particular class, it delegates the request to a parent class loader first, and then looks at its own level if the parent class loader(s) couldn't find the requested class.
So, while classes loaded by a parent class loader (e.g. the server class loader) are visible from a child class loader (e.g. the webapp class loader), the inverse is not true.
In other words, the class loader used to load the Hibernate classes cannot see classes from the webapp and thus cannot load the driver if you don't put it at the same level (or higher in the hierarchy).
To sum up, the only way to keep your driver at the webapp level would be to also bundle the Hibernate jars in the webapp and to disable parent delegation so that the Hibernate JARs from the webapp would get used instead the ones from JBoss.
In which scenarios are we expected to use the default hibernate jars (...)
Typically when using JPA with a container-managed entity manager.
Resources
- How classloading works in JBoss
- How to configure classloaders in JBoss
精彩评论