spring hibernate No Hibernate Session bound to thread异常
来源:互联网 发布:个股数据查询 编辑:程序博客网 时间:2024/05/25 23:29
解决方法
1 Service上加上@Transactional,这样就开启了事务,但是对于查询类的方法,他们不需要事务,怎么办呢?用下面的方法
2 <property name="hibernateProperties">
<props>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
具体解释:
getHibernateTemplate().getSessionFactory().getCurrentSession()的意思是得到当前线程 绑定的session,而当前线程绑定的session是通过当前的事务产生的,如果你没有配置事务的话,当前线程threadlocal中就不存在 session,这样就出现no session错误。
而execute的回调方法,看源码HibernateTemplate中写道
其中getSession,代码如下
其中默认private boolean alwaysUseNewSession = false,所以代码会走到else if (isAllowCreate())
注意这里:else if (isAllowCreate()),其中在HibernateTemplate类中默认private boolean allowCreate = true;
意思说如果当前线程中的session不存在的话,是否允许创建,而默认是允许的,通过函数名字就很清楚,接下来是创建当前线程中的session的代码,所以在没有事务的状态下,用execute回调方法,就不会出现上述问题。
而execute的回调方法,看源码HibernateTemplate中写道
- Java code
- 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,代码如下
- Java code
- 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回调方法,就不会出现上述问题。
- spring hibernate No Hibernate Session bound to thread异常
- Spring+Hibernate 异常No Hibernate Session bound to thread, and configuration does not allow creation
- 异常:No Hibernate Session bound to thread……
- No Hibernate Session bound to thread
- No Hibernate Session bound to thread
- HibernateException: No Hibernate Session bound to thread
- springmvc No Hibernate Session bound to thread
- HibernateException: No Hibernate Session bound to thread
- hibernate的session的No Hibernate Session bound to thread
- No Hibernate Session bound to thread、No Session found f
- org.hibernate.HibernateException: No Hibernate Session bound to thread, and
- org.hibernate.HibernateException: No Hibernate Session bound to thread
- org.hibernate.HibernateException: No Hibernate Session bound to thread
- org.hibernate.HibernateException: No Hibernate Session bound to thread
- Liferay工程调用RoleLocalServiceUtil方法报no hibernate session bound to Thread 异常的解决方法
- No Hibernate Session bound to thread纠结了很久
- No Hibernate Session bound to thread, and configuration
- No Hibernate Session boundNo Hibernate Session bound to thread, and configuration does not allow cr
- WinCE 下隐藏任务栏
- 用户空间与内核空间数据交换的方式(7)------sysctl
- 嵌入式笔记
- ifcfg-eth0 配置
- reap linux zombie process 僵尸进程
- spring hibernate No Hibernate Session bound to thread异常
- Dialog 顶部黑线问题
- u盘做系统
- C#阻止窗体关闭的方法
- sql server存储过程
- C#入门经典学习11-集合比较转换
- gitosis的使用
- Struts2全局异常
- linux 单网卡来绑定多IP实现多网段访问以及多网卡绑定单IP实现附载均衡