- 浏览: 663288 次
- 性别:
- 来自: 杭州
-
文章分类
最新评论
-
HkEndless:
不好意思,请问这确定是回调机制吗。你的例子中只是将接口的实现类 ...
Spring CallBack回调机制介绍 -
hanmiao:
写的真乱啊,完全不知所云...
Java如何调用可执行文件和批处理命令 -
junia_1:
junia_1 写道 shock: ...
为什么要使用EJB -
junia_1:
shock:
为什么要使用EJB -
coollifer:
不错
SQL Server数据导入到Oracle中的方法
java
.lang
.IllegalStateException: No
Hibernate
Session
bound
to
thread
, and
configuration
does
not
allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java
:350)
at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java
:200)
at com.sea.user.dao.BaseDAO.getSession(BaseDAO.java
:23)
at com.sea.user.dao.impl.UserDAO.saveUser(UserDAO.java
:16)
at com.sea.user.service.impl.UserService.saveUser(UserService.java
:14)
at com.sea.user.action.UserAction.register(UserAction.java
:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java
:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java
:25)
====================
意思是说你没有事务,无法产生Session与当前线程绑定,这通常是getHibernateTemplate() getSession()产生的问题,does not allow creation of non-transactional one here 不允许创建非事务,看过一些别人的见解,是说这里的Session必须和事务关联才能产生,(然后绑定到线程,进而在事务处理过程中Session只是唯 一的,这是我想的,保证在一次操作中是事务性的,Session(即一个连接唯一 )才可能,)而用HibernateCallback()就不会出现问题,然而很多人可能直接在Service里想得到 Session.
==================
reference to link:
https://forums.hibernate.org/viewtopic.php?p=2400339&sid=981dbe2a38a5e3a8b031bdbb8edde389
http://www.iteye.com/problems/33765
============================
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回调方法,就不会出现上述问题。
发表评论
-
Spring 多重继承 Dao注入方式
2011-11-15 06:19 1747在用spring开发时,service层配置文件中在每个ser ... -
Transaction marked as rollback only异常
2011-08-15 15:53 10491今天在进行数据库操作时出现:Transaction rolle ... -
Spring Aop的一点理解
2011-06-18 10:47 1000Cross Cutting Concern 是一种独立服务, ... -
Spring Bean 各种集合类型注入介绍
2011-06-17 18:59 1614类结构如下之代码 package com.japie ... -
spring bean 日期格式注入的几种模式
2011-06-17 18:44 62681 当我们进行bean注入到spring当中的时候,这个bea ... -
Spring CallBack回调机制介绍
2011-06-16 19:48 8034所谓回 调,就是客户程序C调用服务程序S中的某个函数A,然后 ... -
在spring中使用Quartz配置job的方式
2011-06-07 00:46 2891定时批处理作业是J2EE企业应用里很重要的一环,用来在晚间 ... -
java程序中获得spring管理的bean的方法
2011-06-07 00:40 1619读取xml文件 /** ... -
spring与struts2整合实现
2011-06-07 00:39 1024在Struts2中整合Spring的IoC支持是一件十分简 ... -
Spring学习笔记
2011-06-07 00:32 1129Proxy (代理) 静态代理 静态代理只需写一 ... -
Spring 注解@Component,@Service,@Controller,@Repository
2011-06-05 05:33 1377pring 2.5 中除了提供 @Component ... -
Spring 让 LOB 数据操作变得简单易行
2011-04-04 22:10 909概述 LOB 代表大对象 ... -
Creating DAOs with Hibernate 3.5 & JPA 2.0 Annotations
2011-04-04 01:23 1052Creating Good DAOs with Hiberna ... -
Spring与Hibernate集成中的session问题讨论
2011-04-03 20:48 14871.通过getSession()方法获得session进行操 ... -
Spring与Hibernate集成中的session问题
2011-04-03 02:50 10611.通过getSession()方法获得session进行操作 ... -
Spring整合Hibernate
2011-04-03 02:09 1386采用声明式事务 1 、声明式事务配置 ... -
SSH整合中could not initialize proxy - no Session问题的OpenSessionInView解决办法
2011-04-03 02:05 2060问题的解决方法主要有两种: 1、将<many-to ... -
在Spring中使用JTA事务管理
2011-04-03 00:35 1519Spring 通过AOP技术可以让我们在脱离EJB的情况下享受 ... -
Spring事务传播机制
2011-04-02 02:08 1069Spring事务传播机制 概述 ... -
Spring常用标记记录
2010-09-04 12:21 1243当开发者未指定id或name属性时,Spring会为这类受管B ...
相关推荐
然而,在使用 Hibernate 进行数据库操作时,经常会遇到 "No Hibernate Session bound to thread" 的错误信息。本文将详细介绍该错误的解决方案。 错误原因 "No Hibernate Session bound to thread" 错误信息通常是...
然而,在某些特定场景下,我们可能并不需要频繁地打开和关闭Session,这时“Hibernate-nosession”就显得尤为重要。本文将深入探讨Hibernate-nosession的概念、应用场景以及如何在实际代码中实现。 首先,理解什么...
hibernate就会抛出: No Hibernate Session bound to thread, and configuration does not allow creation of one here}异常。 在实际的SSH web应用开发中,我们通常用spring来进行事务的管理。我们一般不会在dao层...
4. Hibernate框架错误:文档还包含了“org.hibernate.HibernateException: No Hibernate Session bound to thread”等错误信息,这是Hibernate框架常见的异常,表明在当前线程上没有绑定Hibernate Session。...
当遇到`no current session bound to current context`错误时,通常是因为事务管理方式不正确。Hibernate提供了多种会话上下文配置,如`thread`、`jta`等。在这种情况下,推荐使用`thread`配置,确保每个线程都有...