关于出现No Hibernate Session bound to thread问题的记录
2012-02-15 20:14
567 查看
Spring的getHibernateTemplate().getSessionFactory().getCurrentSession()的意思是得到当前线程绑定的session,而当前线程绑定的session是通过当前的事务产生的,如果没有配置事务的话,当前线程threadlocal中就不存在 session,这样就出现no session错误。
而execute的回调方法,看源码HibernateTemplate中写道
public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");
Session session = getSession();
boolean existingTransaction = (!isAlwaysUseNewSession() &&
(!isAllowCreate() || SessionFactoryUtils.isSessionTransactional(session, getSessionFactory())));
其中getSession,代码如下
protected Session getSession() {
if (isAlwaysUseNewSession()) {
return SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor());
}
else if (isAllowCreate()) {
return SessionFactoryUtils.getSession(
getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
}
else {
try {
return getSessionFactory().getCurrentSession();
}
catch (HibernateException ex) {
throw new DataAccessResourceFailureException("Could not obtain current Hibernate Session", ex);
}
}
其中默认private boolean alwaysUseNewSession = false,所以代码会走到else if (isAllowCreate())
注意这里:else if (isAllowCreate()),其中在HibernateTemplate类中默认private boolean allowCreate = true;其实通过函数名字就很清楚。意思说如果当前线程中的session不存在的话,是否允许创建,而默认是允许的,接下来是创建当前线程中的session的代码,所以在没有事务的状态下,用execute回调方法,就不会出现上述问题。
而execute的回调方法,看源码HibernateTemplate中写道
public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");
Session session = getSession();
boolean existingTransaction = (!isAlwaysUseNewSession() &&
(!isAllowCreate() || SessionFactoryUtils.isSessionTransactional(session, getSessionFactory())));
其中getSession,代码如下
protected Session getSession() {
if (isAlwaysUseNewSession()) {
return SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor());
}
else if (isAllowCreate()) {
return SessionFactoryUtils.getSession(
getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
}
else {
try {
return getSessionFactory().getCurrentSession();
}
catch (HibernateException ex) {
throw new DataAccessResourceFailureException("Could not obtain current Hibernate Session", ex);
}
}
其中默认private boolean alwaysUseNewSession = false,所以代码会走到else if (isAllowCreate())
注意这里:else if (isAllowCreate()),其中在HibernateTemplate类中默认private boolean allowCreate = true;其实通过函数名字就很清楚。意思说如果当前线程中的session不存在的话,是否允许创建,而默认是允许的,接下来是创建当前线程中的session的代码,所以在没有事务的状态下,用execute回调方法,就不会出现上述问题。
相关文章推荐
- 关于出现No Hibernate Session bound to thread问题的记录
- 关于: No Hibernate Session bound to thread, and configuration does not allow creation of non-transacti
- 解决一个Sping管理hibernate时的事务问题(No Hibernate Session bound to thread)
- 解决No Hibernate Session bound to thread, and configuration does not allow create tansactional的问题
- 解决No Hibernate Session bound to thread, and configuration does not allow create tansactional的问题
- getCurrentSession 出现No hibernate bound to the thread
- org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not
- No Hibernate Session bound to thread, and configuration does not allow creation of non-transactiona
- org.hibernate.HibernateException: No Hibernate Session bound to thread
- java开源框架集成常见错误之No Hibernate Session bound to thread
- No Hibernate Session bound to thread, and configuration does not allow creation of non-transactiona
- org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a
- org.hibernate.HibernateException: No Hibernate Session bound to thread, and
- org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a
- No Hibernate Session bound to thread, and configuration does not allow creat
- No Hibernate Session bound to thread, and configuration does not allow
- 关于在Spring中Hibernate的No Session found for current thread问题
- No Hibernate Session bound to thread纠结了很久
- HibernateException: No Hibernate Session bound to thread
- No Hibernate Session bound to thread, and configuration