Hibernate 中的托管会话范围与 jta 会话范围

发布于 2024-11-05 15:56:47 字数 481 浏览 5 评论 0原文

我正在阅读《Java Persistence with Hibernate》一书,并被困在第 494 页。我们通过将 hibernate.current_session_context_class 设置为托管来扩展对话;这是否意味着现在我不能在我的方法中使用“jta(或 javax.jta.UserTransaction)”?

当我查看第 494 页的拦截器代码时,这种假设变得更加强烈。在拦截器中,我们调用“session.beginTransaction”,这意味着我们正在使用 Hibernate Transaction API 来控制事务。

这是否意味着我的实体无法使用 UserTransaction.begin()/commit() 来控制事务?

我之所以这么说,是因为我什至在本书的某个地方读到,当您使用 UserTransaction 来控制事务边界时,您不应该使用 Hibernate Transaction API?

这里需要一些帮助。

I was going through 'Java Persistence with Hibernate' book and got stuck at Page#494. We do extending a conversation by setting hibernate.current_session_context_class to managed; does that mean now I cannot use 'jta (or javax.jta.UserTransaction)' inside my methods?

This assumptions is getting more stronger when I looked at the interceptor code at Page#494. In the interceptor, we are calling 'session.beginTransaction' that means we are using Hibernate Transaction API to control the transaction.

Does this mean, my Entities cannot use UserTransaction.begin()/commit() to control the transaction?

I'm telling this because somewhere in this book I even read that when you use UserTransaction to control the transaction boundaries, you're not suppose to use Hibernate Transaction APIs?

Need some help here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

薄荷→糖丶微凉 2024-11-12 15:56:47

首先,恕我直言,你提到的这本书很棒,但现在有点过时了。但我可以有把握地猜测,您的问题是基于所谓的“Hibernate 中的上下文会话”。它基本上与您希望 Hibernate 管理事务范围的上下文有关。此上下文驱动 sessionFactory.getCurrentSession() 方法的机制。

hibernate.current_session_context_class 的三种可能的配置:

  1. “jta”上下文 = 已经存在的 jta 事务
  2. “线程”上下文 = 当前线程(认为 ThreadLocal)
  3. “托管”上下文 = 自定义到您的域

另外:

  • “jta”和“线程”受支持hibernate 开箱即用的
  • “线程”上下文用于大多数独立的 hibernate 应用程序或基于轻量级框架的应用程序,例如 spring
  • “jta”,用于 Java EE 环境中

现在查看这两个很棒的链接:

http://community.jboss.org/wiki/SessionsAndTransactions

http://community.jboss.org/wiki/OpenSessionInView

第一个更新清晰info 和后者查看“托管”上下文的自定义实现。这会清除很多事情。无论如何:

我正在阅读《Java Persistence with Hibernate》一书,并得到了
卡在第 494 页。我们通过设置来扩展对话
hibernate.current_session_context_class 为托管;这是否意味着现在
我无法在我的方法中使用“jta(或 javax.jta.UserTransaction)”?

您可以 - 因为它是托管上下文,所以它取决于您的自定义实现。

这是否意味着我的实体无法使用
UserTransaction.begin()/commit()来控制事务?

他们可以——事实上,这正是您在“jta”和“线程”上下文中要做的事情。

Firstly, IMHO the book you mentioned is great but a bit outdated now. But I can safely guess, your question is based on whats known as 'Contextual Sessions in Hibernate'. It's basically about context in which you want Hibernate to manage the scope of your transactions. This context drives the mechanics of sessionFactory.getCurrentSession() method.

Three possible configs for hibernate.current_session_context_class:

  1. "jta" context = an already existing jta transaction
  2. "thread" context = current thread (think ThreadLocal)
  3. "managed" context = custom to your domain

Also:

  • "jta" and "thread" are supported by hibernate out of box
  • "thread" context is used in most stand alone hibernate apps or those based on light weight frameworks like spring
  • "jta" is used in Java EE environments

Now check out these two great links:

http://community.jboss.org/wiki/SessionsAndTransactions

http://community.jboss.org/wiki/OpenSessionInView

The first one for updated clear info and latter to see a custom implementation for "managed" context. This'll clear a lots of things. Anyways:

I was going through 'Java Persistence with Hibernate' book and got
stuck at Page#494. We do extending a conversation by setting
hibernate.current_session_context_class to managed; does that mean now
I cannot use 'jta (or javax.jta.UserTransaction)' inside my methods?

You can - as it's managed context it would depend on your custom implementation.

Does this mean, my Entities cannot use
UserTransaction.begin()/commit() to control the transaction?

They can - in fact this is precisely what you'll do in "jta" and "thread" contexts.

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