简体   繁体   English

在Hibernate中管理vs jta会话范围

[英]managed vs jta session scope in Hibernate

I was going through 'Java Persistence with Hibernate' book and got stuck at Page#494. 我正在阅读“Java Persistence with Hibernate”一书并被困在第494页。 We do extending a conversation by setting hibernate.current_session_context_class to managed; 我们通过将hibernate.current_session_context_class to managed;设置hibernate.current_session_context_class to managed;扩展会话hibernate.current_session_context_class to managed; does that mean now I cannot use ' jta (or javax.jta.UserTransaction) ' inside my methods? 这是否意味着我现在不能在我的方法中使用' jta (or javax.jta.UserTransaction) '?

This assumptions is getting more stronger when I looked at the interceptor code at Page#494. 当我查看第494页的拦截器代码时,这种假设变得越来越强烈。 In the interceptor, we are calling ' session.beginTransaction ' that means we are using Hibernate Transaction API to control the transaction. 在拦截器中,我们调用' session.beginTransaction ',这意味着我们正在使用Hibernate Transaction API来控制事务。

Does this mean, my Entities cannot use UserTransaction.begin()/commit() to control the transaction? 这是否意味着,我的实体不能使用UserTransaction.begin()/ commit()来控制事务?

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? 我告诉这个是因为在本书的某处我甚至读到当你使用UserTransaction来控制事务边界时,你不应该使用Hibernate Transaction API吗?

Need some help here. 这里需要一些帮助。

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'. 但我可以肯定地猜测,你的问题是基于被称为“Hibernate中的语境会话”的问题。 It's basically about context in which you want Hibernate to manage the scope of your transactions. 它基本上是关于您希望Hibernate管理事务范围的上下文。 This context drives the mechanics of sessionFactory.getCurrentSession() method. 此上下文驱动sessionFactory.getCurrentSession()方法的机制。

Three possible configs for hibernate.current_session_context_class: hibernate.current_session_context_class有三种可能的配置:

  1. "jta" context = an already existing jta transaction “jta”context =已经存在的jta事务
  2. "thread" context = current thread (think ThreadLocal) “thread”context =当前线程(想想ThreadLocal)
  3. "managed" context = custom to your domain “托管”上下文=自定义您的域名

Also: 也:

  • "jta" and "thread" are supported by hibernate out of box 休眠开箱即可支持“jta”和“thread”
  • "thread" context is used in most stand alone hibernate apps or those based on light weight frameworks like spring “线程”上下文用于大多数独立的hibernate应用程序或基于轻量级框架(如spring)的应用程序
  • "jta" is used in Java EE environments “jta”用于Java EE环境

Now check out these two great links: 现在看看这两个很棒的链接:

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

http://community.jboss.org/wiki/OpenSessionInView 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. 我正在阅读“Java Persistence with Hibernate”一书并被困在第494页。 We do extending a conversation by setting hibernate.current_session_context_class to managed; 我们通过将hibernate.current_session_context_class设置为managed来扩展会话; does that mean now I cannot use 'jta (or javax.jta.UserTransaction)' inside my methods? 这是否意味着我现在不能在我的方法中使用'jta(或javax.jta.UserTransaction)'?

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? 这是否意味着,我的实体不能使用UserTransaction.begin()/ commit()来控制事务?

They can - in fact this is precisely what you'll do in "jta" and "thread" contexts. 他们可以 - 实际上这正是你在“jta”和“线程”背景下所做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM