Hibernate 中的托管会话范围与 jta 会话范围
我正在阅读《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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,恕我直言,你提到的这本书很棒,但现在有点过时了。但我可以有把握地猜测,您的问题是基于所谓的“Hibernate 中的上下文会话”。它基本上与您希望 Hibernate 管理事务范围的上下文有关。此上下文驱动 sessionFactory.getCurrentSession() 方法的机制。
hibernate.current_session_context_class 的三种可能的配置:
另外:
现在查看这两个很棒的链接:
http://community.jboss.org/wiki/SessionsAndTransactions
http://community.jboss.org/wiki/OpenSessionInView
第一个更新清晰info 和后者查看“托管”上下文的自定义实现。这会清除很多事情。无论如何:
您可以 - 因为它是托管上下文,所以它取决于您的自定义实现。
他们可以——事实上,这正是您在“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:
Also:
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:
You can - as it's managed context it would depend on your custom implementation.
They can - in fact this is precisely what you'll do in "jta" and "thread" contexts.