- 浏览: 646335 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
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 1698在用spring开发时,service层配置文件中在每个ser ... -
Transaction marked as rollback only异常
2011-08-15 15:53 10369今天在进行数据库操作时出现:Transaction rolle ... -
Spring Aop的一点理解
2011-06-18 10:47 974Cross Cutting Concern 是一种独立服务, ... -
Spring Bean 各种集合类型注入介绍
2011-06-17 18:59 1582类结构如下之代码 package com.japie ... -
spring bean 日期格式注入的几种模式
2011-06-17 18:44 62001 当我们进行bean注入到spring当中的时候,这个bea ... -
Spring CallBack回调机制介绍
2011-06-16 19:48 7962所谓回 调,就是客户程序C调用服务程序S中的某个函数A,然后 ... -
在spring中使用Quartz配置job的方式
2011-06-07 00:46 2861定时批处理作业是J2EE企业应用里很重要的一环,用来在晚间 ... -
java程序中获得spring管理的bean的方法
2011-06-07 00:40 1573读取xml文件 /** ... -
spring与struts2整合实现
2011-06-07 00:39 990在Struts2中整合Spring的IoC支持是一件十分简 ... -
Spring学习笔记
2011-06-07 00:32 1050Proxy (代理) 静态代理 静态代理只需写一 ... -
Spring 注解@Component,@Service,@Controller,@Repository
2011-06-05 05:33 1308pring 2.5 中除了提供 @Component ... -
Spring 让 LOB 数据操作变得简单易行
2011-04-04 22:10 838概述 LOB 代表大对象 ... -
Creating DAOs with Hibernate 3.5 & JPA 2.0 Annotations
2011-04-04 01:23 1022Creating Good DAOs with Hiberna ... -
Spring与Hibernate集成中的session问题讨论
2011-04-03 20:48 14501.通过getSession()方法获得session进行操 ... -
Spring与Hibernate集成中的session问题
2011-04-03 02:50 10081.通过getSession()方法获得session进行操作 ... -
Spring整合Hibernate
2011-04-03 02:09 1317采用声明式事务 1 、声明式事务配置 ... -
SSH整合中could not initialize proxy - no Session问题的OpenSessionInView解决办法
2011-04-03 02:05 2029问题的解决方法主要有两种: 1、将<many-to ... -
在Spring中使用JTA事务管理
2011-04-03 00:35 1460Spring 通过AOP技术可以让我们在脱离EJB的情况下享受 ... -
Spring事务传播机制
2011-04-02 02:08 994Spring事务传播机制 概述 ... -
Spring常用标记记录
2010-09-04 12:21 1214当开发者未指定id或name属性时,Spring会为这类受管B ...
相关推荐
No Hibernate Session bound to thread,没有绑定线程解决办法,找了好久才找到的
Hibernate-nosession浅谈代码案例。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
hibernate就会抛出: No Hibernate Session bound to thread, and configuration does not allow creation of one here}异常。 在实际的SSH web应用开发中,我们通常用spring来进行事务的管理。我们一般不会在dao层...
本案例主要是分别实现Spring与jdbc和hibernate结合的事务控制,如果该案例有出入的地方,请给我留言,我们一起交流
今天在处理bug的时候发现某一个很奇怪的问题,在执行某类操作的时候会报:Invalid parameter number: no parameters were bound,但是该问题在本地或是测试机上测试都没有重现,猜想是否和版本有关,于是就google了...
1. Introduction to Hibernate 1.1. Preface 1.2. Part 1 - The first Hibernate Application 1.2.1. The first class 1.2.2. The mapping file 1.2.3. Hibernate configuration 1.2.4. Building with Ant 1.2.5. ...
在spring+hibernate的框架中定时操作数据库,主要是拿到sessionFactory,不会出现no session 和transaction no-bound等问题,由sessionFactory完成对数据的操作,有些包是没有用的,有兴趣的可以自己删除掉
错误信息: XXXX client has already been bound to another coroutine使用协程客户端时出现以下错误信息:re
配置tomcat7sqlserver数据连接池java 利用数据源对象建立数据库连接时的错误: Name jdbc is not bound in this Context 解决方案
Name salesDataSource is not bound in this Context,连接池的问题
On a search problem related to branch-and-bound procedures.pdfOn a search problem related to branch-and-bound procedures.pdfOn a search problem related to branch-and-bound procedures.pdfOn a search ...
Android Service之bound实现
Beej's Guide to Network Programming has been one of the top socket programming guides on the Internet for the last 15 years, and it's now for the first time available as a lovingly bound paperback ...
branch and bound
ADO Data Bound Class WizardADO数据类向导
lower_bound函数lower_bound函数用法及应用领域和案例分享lower_bound函数用法及应用领域和案例分享lower_bound函数用法及应用领域和案例分享lower_bound函数用法及应用领域和案例分享lower_bound函数用法及应用领域...
upper confidence bound implementation
NULL 博文链接:https://samson7b.iteye.com/blog/1393458
本推理系统可用于命题、谓词、注解命题和注解谓词的自动推理,系统自动识别是何种类型推理。 <br>注解命题和注解谓词的推理可以解决一般的推理中的不协调问题(即系统中存在矛盾)。