你是否正在寻找关于transactional的内容?让我把最实时的东西奉献给你:

Spring @transactional声明式事务管理 getCurrentSession
在Spring @transactional声明式事务管理的配置中,hibernate.current_session_context_class=thread…
这一句是不能加的…加了就会出错..那为什么不能加呢?
那是因为在Spring事务管理中,current Session是绑定到SpringSessionContext中的,而不是ThreadLocalSessionContext中的
先结合bernate4.0说说:
从开 始,SessionFactory.getCurrentSession()的后台实现是可拔插的。因此,我们引入了新的扩展接口 (org.hibernate.context.spi.CurrentSessionContext)和
新的配置参数(hibernate.current_session_context_class),以便对什么是“当前session”的范围和上下文(scope and context)的定义进行拔插。
它定义 了单一的方法,currentSession(),特定的实现用它来负责跟踪当前的上下文session。
这个接口仅有一个方法:
SessioncurrentSession()
throws HibernateException
Retrieve thecurrent session according to the scoping defined by this implementation.
currentSession()表示 根据当前CurrentSessionContext的实现及定义返回”当前的Session”
这个接口…Hibernate中有3个类实现了这个接口
All Known Implementing Classes:
JTASessionContext,ManagedSessionContext,ThreadLocalSessionContext
1: org.hibernate.context.internal.ThreadLocalSessionContext - 当前session通过当前执行的线程来跟踪和界定。
2: org.hibernate.context.internal.JTASessionContext- 当前session根据JTA来跟踪和界定。这和以前的仅支持JTA的方法是完全一样的。
3: org.hibernate.context.internal.ManagedSessionContext..
Spring为事务管理,也实现了此接口:
1: org.springframework.orm.hibernate4.SpringSessionContext– 当前Session根据Spring和事务管理器来跟踪和界定.
这几种实现都提供了“每事务对应一个session”的编程模型,也称作每次请求一个session。Hibernate session的起始和终结由事务的生存来控制,。
hibernate.current_session_context_class 配置参数定义了应该采用哪个org.hibernate.context.spi.CurrentSessionContext实现。
一般而言,此参数的指明了要使用的实 现类的全名,但那两个内置的实现可以使用简写,即"jta"和"thread"。
hibernate.current_session_context_class=thread
实质是:
hibernate.current_session_context_class= org.hibernate.context.internal.ThreadLocalSessionContext
同理:
hibernate.current_session_context_class=jta
实质是:
hibernate.current_session_context_class= org.hibernate.context.internal.JTASessionContext
而在Spring @transactional声明式事务管理,”currentSession”的定义为: 当前被 Spring事务管理器 管理的Session,此时应配置:
hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
spring 整合hibernate管理事务后,由Spring的TransactionManager管理事务后, currentSession是绑定到SpringSessionContext的,而不是thread。
此时hibernate.current_session_context_class应该是SpringSessionContext,而它又会在使用LocalSessionFactoryBean时自动的设置。
所以就不需要你去设置current_session_context_class
- - - -- -
下面我们来分析一下SessionFactoryImpl, org.hibernate.context.spi.CurrentSessionContext
org.hibernate.context.internal.ThreadLocalSessionContext
org.springframework.orm.hibernate4.SpringSessionContext
这些类的源代码
1: 分析sessionFactory.getCurrentSession() 我们跟进去
来到SessionFactoryImpl.getCurrentSession()方法:
public final class SessionFactoryImpl implements SessionFactoryImplementor { . . . private final transient CurrentSessionContext currentSessionContext; . . . public Session getCurrentSession() throws HibernateException { if ( currentSessionContext == null ) { throw new HibernateException( "No CurrentSessionContext configured!" ); } return currentSessionContext.currentSession(); } . . . }
SessionFactoryImpl 的currentSessionContext属性的实际类型就是
由hibernate.current_session_context_class决定的…
2:首先设置: hibernate.current_session_context_class= org.hibernate.context.internal.ThreadLocalSessionContext
到这一句,currentSessionContext.currentSession()跟进去
public class ThreadLocalSessionContext implements CurrentSessionContext { . . . private static final ThreadLocal以上就是关于transactional的全部内容,相信你一定会非常满意。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shenmilingyu/article-14154-1.html
然后可以直接干过去