简体   繁体   English

Hibernate JTA多用户会话事务映射

[英]Hibernate JTA multiuser session-transaction mapping

I am referring to https://developer.jboss.org/wiki/SessionsAndTransactions and currently trying to understand demarcation with JTA. 我指的是https://developer.jboss.org/wiki/SessionsAndTransactions ,目前正在尝试了解JTA的划分。 It states that within a particular transaction using getCurrentSession() always gives the same current session. 它指出在特定事务中,使用getCurrentSession()总是给出相同的当前会话。 Does it mean: 它的意思是:

  1. If another user is executing the same piece of code (which fetches a transaction by lookup, then uses getCurrentSession() and then closes the transaction) in another thread - that user will have his own transaction and his own current session ie the current sessions of 2 users are same for themselves but different from each other? 如果另一个用户正在另一个线程中执行同一段代码(该代码通过查找来获取事务,然后使用getCurrentSession()然后关闭该事务)-该用户将拥有自己的事务和自己的当前会话,即2个用户相同但彼此不同?
  2. If 1 is true and based on the code shown in the link for JTA demarcation - how does the system (read Hibernate) understand which session to respond to which user when getCurrentSession() is used? 如果1为true并且基于JTA划分链接中显示的代码-使用getCurrentSession()时,系统(读取Hibernate)如何理解响应哪个用户的会话? After all we don't pass the transaction as argument to getCurrentSession(). 毕竟,我们不会将事务作为参数传递给getCurrentSession()。

Any pointers/help is much appreciated. 任何指针/帮助深表感谢。

Thanks 谢谢

According to javadoc SessionFactory.getCurrentSession() 根据javadoc SessionFactory.getCurrentSession()

The definition of what exactly "current" means controlled by the {@link org.hibernate.context.spi.CurrentSessionContext} impl configured for use. “当前”的确切定义由配置为使用的{@link org.hibernate.context.spi.CurrentSessionContext} impl控制。

JTA is configured this will default to the {@link org.hibernate.context.internal.JTASessionContext} impl. 已配置JTA,这将默认为{@link org.hibernate.context.internal.JTASessionContext}的实现。

Then you can see JTASessionContext javadoc and implementation. 然后您可以看到JTASessionContext javadoc及其实现。

If a session is not already associated with the current JTA transaction at the time {@link #currentSession()} is called, a new session will be opened and it will be associated with that JTA transaction. 如果在调用{@link #currentSession()}时会话尚未与当前JTA事务相关联,则将打开一个新会话并将其与该JTA事务相关联。

public Session currentSession() throws HibernateException {
  ...
  final TransactionManager transactionManager = jtaPlatform.retrieveTransactionManager();
  ...
  txn = transactionManager.getTransaction();
  ...
  final Object txnIdentifier = jtaPlatform.getTransactionIdentifier( txn );
  ...
  Session currentSession = currentSessionMap.get( txnIdentifier );
  ...
}

TransactionManager javadoc TransactionManager javadoc

Internally, the transaction manager associates transactions with threads, and the methods here operate on the transaction associated with the calling thread. 在内部,事务管理器将事务与线程相关联,并且此处的方法对与调用线程相关联的事务进行操作。

So, it's similar (but more clearly) to Sessions and Transactions/Transaction demarcation with plain JDBC : 因此,它与普通JDBC的会话和事务/事务划分类似(但更清楚):

In other words, the session is bound to the thread behind the scenes, but scoped to a transaction, just like in a JTA environment. 换句话说,会话绑定到幕后线程,但作用域是事务,就像在JTA环境中一样。

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

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