通过 JNDI JBoss 6 进行休眠 - 正确完成吗?

发布于 2024-11-05 09:57:49 字数 1528 浏览 5 评论 0原文

我已经按照很多文章在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瘫痪情歌 2024-11-12 09:57:49

我相信您没有正确地将 Hibernate 与 JBoss JTA 一起使用,但 Hibernate 可能会自动消耗现有的 JTA 事务。为了确保这一点,请尝试增加 Hibernate 的日志记录详细程度(仅对于 org.hibernate.transaction 就足够了)并查找如下条目:

16:27:11,518  INFO TransactionFactoryFactory:58 - Using default transaction strategy (direct JDBC transactions)
16:27:11,520  INFO TransactionManagerLookupFactory:79 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)

如果您看到类似上面的条目,那么您将需要显式地将属性 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:

16:27:11,518  INFO TransactionFactoryFactory:58 - Using default transaction strategy (direct JDBC transactions)
16:27:11,520  INFO TransactionManagerLookupFactory:79 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)

If you see entries like the above, then you'll need to explicitly set the property transaction.factory_class to org.hibernate.transaction.JTATransactionFactory and the property jta.UserTransaction to java: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文