JBOSS 上用于休眠的 JDBC jar 的位置
我正在使用 jboss 5.1.0 GA,并使用 hibernate 从我的应用程序连接到 postgresql 数据库。我正在使用 jboss 中找到的默认 hibernate jar。 在我的应用程序战争中,我只打包应用程序特定的 jar 和其他在 jboss 中找不到的第 3 方 jar。
如果我将 postgres jdbc jar 放在 server/xxxx/lib 下,它就会被选中。
如果我将它放在应用程序 war 文件的 web-inf/lib 下,它不会被选中,并且我得到 -
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:545)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
我的问题是如何让 jboss 从 web-inf/lib 下加载 postgres 驱动程序 jar?
就像我说的,hibenate jar 是来自 jboss 的,我没有将它们打包到我的应用程序中。
更新:另一个问题。在哪些场景中我们希望使用默认的 hibernate jar,在哪些可能的场景中我们希望将 hibernate jar 与我们的应用程序一起打包?
谢谢,
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.hibernate.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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,这是不可能的。简单来说,类加载器是以父子树的形式排列的。当类加载器被要求加载特定的类时,它首先将请求委托给父类加载器,然后如果父类加载器找不到所请求的类,则查看自己的级别。
因此,虽然父类加载器(例如服务器类加载器)加载的类对于子类加载器(例如 webapp 类加载器)是可见的,但反之则不然。
换句话说,用于加载 Hibernate 类的类加载器无法看到来自 web 应用程序的类,因此如果您不将驱动程序放在同一级别(或层次结构中的更高级别),则无法加载驱动程序。
总而言之,将驱动程序保持在 Web 应用程序级别的唯一方法是将 Hibernate jar 捆绑在 Web 应用程序中,并禁用父委派,以便使用 Web 应用程序中的 Hibernate JAR来自 JBoss 的。
通常在将 JPA 与容器管理的实体管理器一起使用时。
资源
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.
Typically when using JPA with a container-managed entity manager.
Resources