hibernate 的 openSession 和 getCurrentSession 的实现
来源:互联网 发布:php怎么接私活 编辑:程序博客网 时间:2024/06/06 03:36
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 看做复本,对象不一样,但是内容是一样的。
这是一种 空间换时间的 解决同步问题 的 办法。
- hibernate 的 openSession 和 getCurrentSession 的实现
- hibernate 的openSession和getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate中openSession 和 getCurrentSession 的区别
- hibernate的session判断-openSession和getCurrentSession
- Hibernate 中 openSession()和getCurrentSession() 的区别
- Hibernate openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate之openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate: openSession() 和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别 .
- Hibernate的openSession和getCurrentSession区别
- hibernate中 openSession和getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- 【Hibernate】OpenSession和getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Unix编程笔记疑问篇----如何从指令运行的角度去看待系统调用???
- linux命令之top
- 【Leet Code】148. Sort List---Medium
- A标签伪类
- 黑马程序员Android 百分比布局库(percent-support-lib) 解析与扩展
- hibernate 的 openSession 和 getCurrentSession 的实现
- cassandra paging 分页方法应用
- 循环队列-顺序存储结构-数据结构
- java中String和StringBuider,StringBuffer的区别
- @SessionAttributes
- 多线程
- 黑马程序员Android 增强版百分比布局库 为了适配而扩展
- Android开发编码规范导致的内存泄露问题
- 采用广播接收者拦截外拨电话及其特性