`
newposte
  • 浏览: 21016 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类

请大牛看看这个问题!No Hibernate Session bound to thread, and configuration does not allow

阅读更多
小弟刚开始学习Spring 和 Hibernate框架,做了一个Spring和Hibernate的整合,用的是Spring 的MVC。目前老是这个错:No Hibernate Session bound to thread, and configuration does not allow,废话不多少,请看配置!

spring 配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
			 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	     xmlns:jee="http://www.springframework.org/schema/jee" 
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xmlns:context="http://www.springframework.org/schema/context"
	     xmlns:aop="http://www.springframework.org/schema/aop"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
	     					 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
	     					 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd 
	     					 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 
	     					 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
	     
	     default-lazy-init="false">


	<description>Spring公共配置 </description>
		
	<!-- 定义受环境影响易变的变量 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<!-- 标准配置 -->
				<value>classpath*:/application.properties</value>
			</list>
		</property>
	</bean>	

	<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<!-- Connection Info -->
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

		<!-- Connection Pooling Info -->
		<property name="initialSize" value="5" />
		<property name="maxActive" value="100" />
		<property name="maxIdle" value="30" />
		<property name="maxWait" value="1000" />
		<property name="poolPreparedStatements" value="false" />
		<property name="defaultAutoCommit" value="false" />
	</bean>


	<!-- Hibernate配置 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="namingStrategy">
			<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
 				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>  
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hibernate.jdbc.use_scrollable_resultset">false</prop>
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
				<prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>
			</props>
		</property>
		<property name="packagesToScan" value="com.ligang.app.base.*" />
	</bean>

	<!-- 事务管理器配置,单数据源事务 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 使用annotation定义事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
      	
</beans>


Spring MVC的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
			 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	     xmlns:jee="http://www.springframework.org/schema/jee" 
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xmlns:context="http://www.springframework.org/schema/context"
	     xmlns:aop="http://www.springframework.org/schema/aop"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
	     					 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
	     					 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd 
	     					 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 
	     					 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
	     
	     default-lazy-init="false">

    <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
	<context:component-scan base-package="com.ligang.app" />
	
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<!-- 定义Spring MVC 的模板文件 -->

      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>     <!-- 支持JSTL -->
           <property name="prefix" value="WEB-INF/pages/" />     <!-- 模板的路径 -->
           <property name="suffix" value=".jsp" />                 <!-- 模板文件的后缀-->
      </bean>



</beans>



spring Controller 代码:
@Controller
@RequestMapping("/zsdt.do")
public class ZsdtController {

   @Autowired
   private ZsdtService zsdtService;
   private Page<TZsdt> page = new Page<TZsdt>(15);
	
   @SuppressWarnings("unchecked")
   @RequestMapping(params="method=list")
   public String list(ModelMap map,HttpServletRequest request,HttpServletResponse response) {

        String startIndex = request.getParameter("startIndex")==null?"1":request.getParameter("startIndex");
        Integer goToPageNo = request.getParameter("goToPageNo")==null?0:Integer.parseInt(request.getParameter("goToPageNo"));
        page.setFirst(Integer.parseInt(startIndex));

        if(goToPageNo != 0){
        	page.setPageNo(goToPageNo);
        }
        page = zsdtService.queryZsdt(page);        
        request.setAttribute("page", page);
        
        return "zsdt";  

   }
 }



Service层代码:
@Service
@Transactional
public class ZsdtService {
  
	@Autowired
	private ZsdtDao zsdtDao;
	
	@Transactional(readOnly=true)
	public Page<TZsdt> queryZsdt(final Page<TZsdt> page){
		Page<TZsdt> newpage = zsdtDao.findPage(page);
		return newpage;
	}

}



在执行DB层的获取连接的时候报错,我是这么获取数据库连接的:

	/**
	 * 取得当前Session.
	 */
	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}





具体错误信息:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
	at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
	at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:622)
	at com.ligang.modules.orm.hibernate.SimpleHibernateDao.getSession(SimpleHibernateDao.java:101)
	at com.ligang.modules.orm.hibernate.SimpleHibernateDao.createCriteria(SimpleHibernateDao.java:322)
	at com.ligang.modules.orm.hibernate.HibernateDao.findPage(HibernateDao.java:144)
	at com.ligang.app.base.service.ZsdtService.queryZsdt(ZsdtService.java:20)
	at com.ligang.app.base.controller.ZsdtController.list(ZsdtController.java:38)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:421)
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:136)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:326)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:313)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Unknown Source)



请各位大牛看看,小弟不胜感激!!!!
分享到:
评论
1 楼 anyeshouwei 2015-12-30  
我也是报这个错...

相关推荐

    org.hibernate.HibernateException: No Hibernate Session bound to thread

    然而,在使用 Hibernate 进行数据库操作时,经常会遇到 "No Hibernate Session bound to thread" 的错误信息。本文将详细介绍该错误的解决方案。 错误原因 "No Hibernate Session bound to thread" 错误信息通常是...

    Hibernate-nosession

    在Java的持久化框架中,Hibernate是一个非常流行的ORM(对象关系映射)工具,它极大地简化了数据库操作。然而,在某些特定场景下,我们可能并不需要频繁地打开和关闭Session,这时“Hibernate-nosession”就显得尤为...

    OA系统整体设计及约定、搭建环境.rar_OA系统及配置_OA系统整体设计及约定、搭建环境_creation

    hibernate就会抛出: No Hibernate Session bound to thread, and configuration does not allow creation of one here}异常。 在实际的SSH web应用开发中,我们通常用spring来进行事务的管理。我们一般不会在dao层...

    Hibernate Session释放模式

    在处理大量数据或者长时间运行的事务时,合理地管理Session的生命周期至关重要,这就涉及到了Hibernate的Session释放模式。本文将深入探讨Hibernate Session的几种释放模式,以及它们在实际开发中的应用和优缺点。 ...

    JAVA错误文档.pdf

    3. Spring框架异常:文档提到“org.springframework.beans.factory.BeanCreationException”和“No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here...

    spring分别与jdbc和hibernate结合的事务控制--案例

    总之,Spring通过其灵活的事务管理机制,极大地简化了与JDBC和Hibernate集成时的事务控制。无论是在简单还是复杂的业务逻辑中,Spring都能提供可靠的事务管理方案,确保数据的正确性和一致性。通过理解并熟练运用...

    数据池连接Name jdbc is not bound in this Context解决方案

    在这个问题中,开发者遇到了一个常见的错误:“Name jdbc is not bound in this Context”,这通常意味着在Tomcat的环境中,指定的数据源没有被正确地绑定或配置。 要解决这个问题,首先需要在Tomcat的配置文件中...

    Tomcat启动时报错:Name salesDataSource is not bound in this Context

    Name salesDataSource is not bound in this Context,连接池的问题

    分枝定界(branch and bound)解旅行商(TSP)问题

    **分枝定界法(Branch and Bound)**是一种在离散优化问题中寻找全局最优解的算法,尤其在解决旅行商问题(Traveling Salesman Problem, TSP)时展现出其强大的能力。旅行商问题是一个经典的组合优化问题,目标是...

    Hibernate Reference Documentation3.1

    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. ...

    branch and bound

    分支限界法(Branch and Bound, B&B)是解决大规模NP难组合优化问题最广泛使用的工具之一。它是一种算法框架,需要针对具体问题进行填充和完善。即使如此,设计高效B&B算法的原则也已经逐渐成熟起来。 本文将探讨B&...

    使用Hibernate一些常见错误解决办法

    当遇到`no current session bound to current context`错误时,通常是因为事务管理方式不正确。Hibernate提供了多种会话上下文配置,如`thread`、`jta`等。在这种情况下,推荐使用`thread`配置,确保每个线程都有...

    On a search problem related to branch-and-bound procedures.pdf

    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 ...

    hibernate_reference使用指南全

    为了确保 Hibernate 正确处理对象的唯一性,需要为持久化类实现这两个方法。 - **4.4 动态模型** 介绍如何使用 Hibernate 的动态元数据映射来处理结构不确定的数据。 - **4.5 Tuplizer** Tuplizer 是 ...

    微软内部资料-SQL性能优化3

    SQL Server needs to lock data that does not exist! If no rows satisfy the WHERE condition the first time the range is scanned, no rows should be returned on any subsequent scans. Key range locks are ...

    PDO版本问题 Invalid parameter number: no parameters were bound

    然而,当遇到"Invalid parameter number: no parameters were bound"错误时,这意味着尝试执行的SQL语句中的占位符参数没有正确地与实际值绑定。 这个问题在某些旧版本的PHP和PDO中被报告为一个bug,特别是...

    fenzhixianjie.rar_1背包问题_Branch and bound_fenzhixianjie_knapsack

    在这个“fenzhixianjie.rar_1背包问题_Branch and bound_fenzhixianjie_knapsack”中,重点是利用优先队列(通常用二叉堆实现)来改进传统的分支限界法。优先队列允许快速地找到当前最优的分支进行扩展,提高了算法...

    Hibernate 3.x 参考手册

    ### Hibernate 3.x 参考手册关键知识点解析 #### 一、快速入门与Tomcat集成 **1.1 快速开始使用 Hibernate** - **环境准备:** - 使用 Hibernate 前需确保 Java 环境已安装配置。 - 下载 Hibernate 3.x 版本库...

    Branch and Bound 分支界定法

    分支界定法(Branch and Bound,简称B&B)是一种用于求解非凸问题全局优化问题的算法。斯坦福大学的大牛Stephen Boyd在他的课程笔记中对这种方法进行了详细介绍。该方法非启发式,能够为全局最优解提供一个可证明的...

    Spring+hibernate+quartz 定时操作数据库

    在spring+hibernate的框架中定时操作数据库,主要是拿到sessionFactory,不会出现no session 和transaction no-bound等问题,由sessionFactory完成对数据的操作,有些包是没有用的,有兴趣的可以自己删除掉

Global site tag (gtag.js) - Google Analytics