通过 JNDI JBoss 6 进行休眠 - 正确完成吗?
我已经按照很多文章在 JBoss 6.0.0.Final 中通过 JNDI 设置了休眠,并遇到了一些问题,但得到了排序并且它可以工作,但问题是,我做得对吗?对于其中之一,我没有指定事务查找或工厂类。
service-hibernate.xml 文件:
<hibernate-configuration xmlns="urn:jboss:hibernate-deployer:1.0">
<session-factory name="java:/hibernate/SessionFactory" bean="jboss.test.har:service=Hibernate, testcase=TimersUnitTestCase">
<property name="datasourceName">java:jdbc/MyDataSourceDS</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="current_session_context_class">jta</property>
<depends>jboss:service=Naming</depends>
<depends>jboss:service=TransactionManager</depends>
</session-factory>
显然也有我的实体和 .hbm.xml 文件,所以这里是我在 servlet 中使用的一些代码来测试:
UserTransaction utx = (UserTransaction)new InitialContext().lookup("UserTransaction");
utx.begin();
InitialContext ctx = new InitialContext();
SessionFactory sf = (SessionFactory)ctx.lookup("java:/hibernate/SessionFactory");
Session session = sf.getCurrentSession();
List<TblSettings> settings = session.createQuery("FROM TblSettings").list();
utx.commit();
上面的代码可以工作,但是我是否按照预期的方式进行操作?
顺便说一句,我正在使用 Maven HAR 插件将我的Entity+.hbm.xml 和 service-hibernate.xml 打包为 HAR 存档。
谢谢。
I've setup hibernate via JNDI in JBoss 6.0.0.Final by following alot of articles and had some problems but got it sorted and it works, but the question is, have I done this right? for one I have not specified a transaction lookup or factory class.
service-hibernate.xml file:
<hibernate-configuration xmlns="urn:jboss:hibernate-deployer:1.0">
<session-factory name="java:/hibernate/SessionFactory" bean="jboss.test.har:service=Hibernate, testcase=TimersUnitTestCase">
<property name="datasourceName">java:jdbc/MyDataSourceDS</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="current_session_context_class">jta</property>
<depends>jboss:service=Naming</depends>
<depends>jboss:service=TransactionManager</depends>
</session-factory>
Obviously have my entities and .hbm.xml files as well, so here's some code that I'm using in servlet to test with:
UserTransaction utx = (UserTransaction)new InitialContext().lookup("UserTransaction");
utx.begin();
InitialContext ctx = new InitialContext();
SessionFactory sf = (SessionFactory)ctx.lookup("java:/hibernate/SessionFactory");
Session session = sf.getCurrentSession();
List<TblSettings> settings = session.createQuery("FROM TblSettings").list();
utx.commit();
The above code works, but am I doing it the way it was intended?
btw, I'm using the maven HAR plugin the package my entities+.hbm.xml and service-hibernate.xml as a HAR archive.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您没有正确地将 Hibernate 与 JBoss JTA 一起使用,但 Hibernate 可能会自动消耗现有的 JTA 事务。为了确保这一点,请尝试增加 Hibernate 的日志记录详细程度(仅对于
org.hibernate.transaction
就足够了)并查找如下条目:如果您看到类似上面的条目,那么您将需要显式地将属性
transaction.factory_class
设置为org.hibernate.transaction.JTATransactionFactory
并将属性jta.UserTransaction
设置为cfg.xml 中的java:comp/UserTransaction
。还要注意的另一件事是,HAR 部署在 JPA 出现之前非常有用,当时没有以 AS 管理的方式使用 Hibernate 的标准方法。 HAR 部署可能会在 JBoss AS 7 中被弃用。您应该根据您的项目自行决定,但我建议长期考虑迁移到 JPA。
I believe you are not correctly using Hibernate with JBoss JTA, but Hibernate may be automatically consuming existing JTA transactions. To make sure, try to increase the logging verbosity (just for
org.hibernate.transaction
should be enough) for Hibernate and look for entries like this:If you see entries like the above, then you'll need to explicitly set the property
transaction.factory_class
toorg.hibernate.transaction.JTATransactionFactory
and the propertyjta.UserTransaction
tojava:comp/UserTransaction
in your cfg.xml.One more thing to note is that HAR deployments were useful in the pre-JPA days, when there was no standard way of using Hibernate in a AS-managed way. HAR deployments will probably be deprecated in JBoss AS 7. You should decide by yourself based on your project, but I'd recommend to consider migrating to JPA for the long term.