hibernate 的 openSession 和 getCurrentSession 的实现

openSession  :

public org.hibernate.classic.Session openSession() throws HibernateException {
return openSession(interceptor);
}


public org.hibernate.classic.Session openSession(Interceptor sessionLocalInterceptor)
throws HibernateException {
// note that this timestamp is not correct if the connection provider
// returns an older JDBC connection that was associated with a
// transaction that was already begun before openSession() was called
// (don't know any possible solution to this!)
long timestamp = settings.getCacheProvider().nextTimestamp();
return openSession( null, true, timestamp, sessionLocalInterceptor );
}




private SessionImpl openSession(
Connection connection,
   boolean autoClose,
   long timestamp,
   Interceptor sessionLocalInterceptor
) {
return new SessionImpl(
       connection,
       this,
       autoClose,
       timestamp,
       sessionLocalInterceptor == null ? interceptor : sessionLocalInterceptor,
       settings.getDefaultEntityMode(),
       settings.isFlushBeforeCompletionEnabled(),
       settings.isAutoCloseSessionEnabled(),
       settings.getConnectionReleaseMode()
);
}





private SessionImpl openSession(
Connection connection,
   boolean autoClose,
   long timestamp,
   Interceptor sessionLocalInterceptor
) {
return new SessionImpl(
       connection,
       this,
       autoClose,
       timestamp,
       sessionLocalInterceptor == null ? interceptor : sessionLocalInterceptor,
       settings.getDefaultEntityMode(),
       settings.isFlushBeforeCompletionEnabled(),
       settings.isAutoCloseSessionEnabled(),
       settings.getConnectionReleaseMode()
);
}






if ( factory.getStatistics().isStatisticsEnabled() ) {
factory.getStatisticsImplementor().openSession();
}


public interface StatisticsImplementor {
public void openSession();


单例模式: 控制同步
private long sessionOpenCount;
public synchronized void openSession() {
sessionOpenCount++;
}



getCurrentSession:

public org.hibernate.classic.Session getCurrentSession() throws HibernateException;




public org.hibernate.classic.Session getCurrentSession() throws HibernateException {
if ( currentSessionContext == null ) {
throw new HibernateException( "No CurrentSessionContext configured!" );
}
return currentSessionContext.currentSession();
}


public interface CurrentSessionContext extends Serializable {

public org.hibernate.classic.Session currentSession() throws HibernateException;



最后 从 Map 中 取连接 :
public Session currentSession() {
Session current = existingSession( factory );
if ( current == null ) {
throw new HibernateException( "No session currently bound to execution context" );
}
return current;
}

或者

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 );
}
return current;
}



建议你读读下面两个类。它是SessionFactory#getCurrectSession()的实现,它是把Session绑定到了ThreadLocal中。
ThreadLocalSessionContext
CurrentSessionContext

ThreadLocal 作为一个本地变量,当用户来 获取一个新的连接时,从连接池中 给予一个新的连接; 当有很多用户获取连接时,那么 就会有 很多 与session 一一对应 的 ThreadLocal 的变量, 可以把 ThreadLocal 看做复本,对象不一样,但是内容是一样的。


这是一种 空间换时间的 解决同步问题 的 办法。


猜你喜欢

转载自blog.csdn.net/tuyf_hs/article/details/48469189
今日推荐