简体   繁体   中英

managed vs jta session scope in Hibernate

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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