理解hibernate 已ThreadLocal管理session的机制。
2010-09-09 17:10
567 查看
在运用中我们获得sesion 以
Session session = sessionFactory.getCurrentSession();
在这里如果我们采用thread方法对事务进行管理,即采用jdbc事务,那么sessionFactory的底层是通过ThreadLocalSessionContext类来获得session对象代码如下:
public org.hibernate.classic.Session getCurrentSession() throws HibernateException {
if ( currentSessionContext == null ) {
throw new HibernateException( "No CurrentSessionContext configured!" );
}
return currentSessionContext.currentSession();
}
其中currentSessionContext类型为ThreadLocalSessionContext
在ThreadLocalSessionContext的currentSession我们可见:
public final Session currentSession() throws HibernateException {
Session current = existingSession( factory );
if (current == null) {
current = buildOrObtainSession();
// register a cleanup synch
current.getTransaction().registerSynchronization( buildCleanupSynch() );
// wrap the session in the transaction-protection proxy
if ( needsWrapping( current ) ) {
current = wrap( current );
}
// then bind it
doBind( current, factory );//把当前的session和sessionFactory与当前线程关联
}
return current;
}
doBind()方法实现如下:
protected static Map sessionMap() {
return ( Map ) context.get();
}
private static void doBind(org.hibernate.Session session, SessionFactory factory) {
Map sessionMap = sessionMap();//sessionMap方法为获得与当前线程绑定的值,返回为Map类型
if ( sessionMap == null ) {
sessionMap = new HashMap();
context.set( sessionMap );
}
sessionMap.put( factory, session );//把factory和session放入与当前线程绑定的map中,这样,就形成了当前线程与session的传递关联。
}
Session session = sessionFactory.getCurrentSession();
在这里如果我们采用thread方法对事务进行管理,即采用jdbc事务,那么sessionFactory的底层是通过ThreadLocalSessionContext类来获得session对象代码如下:
public org.hibernate.classic.Session getCurrentSession() throws HibernateException {
if ( currentSessionContext == null ) {
throw new HibernateException( "No CurrentSessionContext configured!" );
}
return currentSessionContext.currentSession();
}
其中currentSessionContext类型为ThreadLocalSessionContext
在ThreadLocalSessionContext的currentSession我们可见:
public final Session currentSession() throws HibernateException {
Session current = existingSession( factory );
if (current == null) {
current = buildOrObtainSession();
// register a cleanup synch
current.getTransaction().registerSynchronization( buildCleanupSynch() );
// wrap the session in the transaction-protection proxy
if ( needsWrapping( current ) ) {
current = wrap( current );
}
// then bind it
doBind( current, factory );//把当前的session和sessionFactory与当前线程关联
}
return current;
}
doBind()方法实现如下:
protected static Map sessionMap() {
return ( Map ) context.get();
}
private static void doBind(org.hibernate.Session session, SessionFactory factory) {
Map sessionMap = sessionMap();//sessionMap方法为获得与当前线程绑定的值,返回为Map类型
if ( sessionMap == null ) {
sessionMap = new HashMap();
context.set( sessionMap );
}
sessionMap.put( factory, session );//把factory和session放入与当前线程绑定的map中,这样,就形成了当前线程与session的传递关联。
}
相关文章推荐
- Hibernate关于Session管理的openSession和getCurrentSession的理解
- Hibernate——ThreadLocal模式下管理的Session会在事务提交后自动关闭
- 使用ThreadLocal模式管理Hibernate的Session
- 利用ThreadLocal管理Hibernate的Session
- 利于ThreadLocal模式管理hibernate的Session
- Hibernate关于Session管理的openSession和getCurrentSession的理解
- 如何理解Hibernate的延迟加载机制?在实际应用中,延迟加载与Session关闭的矛盾是如何处理的?
- 如何理解 Hibernate的延迟加载机制?延迟加载与Session关闭的矛盾如何处理?
- Hibernate用ThreadLocal模式 (线程局部变量模式) 管理Session
- 利于ThreadLocal管理Hibernate Session
- Java - 如何理解Hibernate的延迟加载机制?在实际应用中,延迟加载与Session关闭的矛盾是如何处理的?
- 理解Hibernate的Session和缓存机制
- Hibernate关于Session管理的openSession和getCurrentSession的理解
- 用ThreadLocal模式管理Hibernate的session
- Spring和Hibernate整合时对session及事务管理的理解
- 理解hibernate的session缓存机制(一级缓存和二级缓存)
- Tomcat中session的管理机制
- SSH框架之Hibernate的集合缓存、查询缓存、项目中的session管理方式
- 我所理解的Cocos2d-x Cocos2d-x 内存管理机制
- Hibernate学习笔记之----理解对象的持久性与一级缓存机制