org.hibernate.HibernateException: No Session found for current thread

J2EE 码拜 6年前 (2015-04-04) 783次浏览 0个评论

hibernate:4.1.6
spring:4.0.1

 
工程启动没错,进入getAllUser页面获得数据库用户信息时报如下错误: 

严重: Servlet.service() for servlet springMVC threw exception

org.hibernate.HibernateException: No Session found for current thread

at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)

at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:941)

at com.mlf.web.controller.dao.UserDAO.getAllUser(UserDAO.java:27)

at com.mlf.web.controller.service.UserManager.getAllUser(UserManager.java:28)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)

at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)

at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)

at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)

at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)

at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)

at $Proxy19.getAllUser(Unknown Source)

at com.mlf.web.controller.UserController.getAllUser(UserController.java:36)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

———————————————————————————————————————–
hibernate.cfg.test.xml(com.mlf.web.controller.hibernate

<?xml version=""1.0"" encoding=""utf-8""?>

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <mapping class="com.mlf.web.controller.entity.User"/>
    </session-factory>
</hibernate-configuration>

———————————————————————————————————————–
applicationContext.xml(com.mlf.web.controller.spring)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>

<beans>
	<bean id="userDao" class="com.mlf.web.controller.dao.UserDAO">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<bean id="userManagerBase" class="com.mlf.web.controller.service.UserManager">
		<property name="userDao" ref="userDao"></property>
	</bean>

	<bean id="userManager" parent="transactionBese">
		<property name="target" ref="userManagerBase"></property>
	</bean>

</beans>

———————————————————————————————————————–
spring-annotation.xml(包:config)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p" 
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

	<!-- 扫描包 -->
	<context:component-scan base-package="com.mlf.web.controller"></context:component-scan>

	<!-- 开启注解 -->
	<mvc:annotation-driven/>

	<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->

	<!-- 静态资源访问 -->
	<mvc:resources location="/img/" mapping="/img/**"/>
	<mvc:resources location="/js/" mapping="/js/**"/>


	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

	<!-- 配置文件上传解析器 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8"/>
		<!-- 文件最大值 -->
		<property name="maxUploadSize" value="10485760000"/>
		<!-- 缓存大小 -->
		<property name="maxInMemorySize" value="40960"/>
	</bean>

</beans>

———————————————————————————————————————–
spring-core.xml(包:config)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>

<beans>
	<import resource="classpath:com/mlf/web/controller/spring/applicationContext.xml"/>
</beans>

———————————————————————————————————————–
spring-hibernate.xml(包:config)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>

<beans>
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncodeing=utf-8"></property>
		<property name="username" value="root"></property>
		<property name="password" value="donny2014"></property>
	</bean>

	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
				<!-- 自动更新 -->
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<!-- 在控制台打印SQL语句 -->
				<prop key="hibernate.show_sql">true</prop>
				<!-- 将SQL脚本进行格式化后再输出 -->
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>
		<property name="configLocations">
			<list>
				<value>
					classpath:com/mlf/web/controller/hibernate/hibernate.cfg.test.xml
				</value>
			</list>
		</property>
	</bean>

	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!-- 配置事务 -->
	<bean id="transactionBese" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
		<property name="transactionManager" ref="transactionManager"></property>
		<property name="transactionAttributes">
			<props>
				<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="get*">PROPAGATION_NEVER</prop>
			</props>
		</property>
	</bean>

</beans>

———————————————————————————————————————–
UserController.java(包:com.mlf.web.controller)

package com.mlf.web.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mlf.web.controller.entity.User;
import com.mlf.web.controller.service.IUserManager;

@Controller
@RequestMapping("/user")
public class UserController {

	@Resource(name="userManager")
	private IUserManager userManager;

	@RequestMapping("/toAddUser")
	public String toAddUser(){
		return "/addUser";
	}

	@RequestMapping("/addUser")
	public String addUser(User user){

		userManager.addUser(user);

		return "/success";
	}

	@RequestMapping("/getAllUser")
	public String getAllUser(HttpServletRequest request){
		List<User> user = userManager.getAllUser();

		request.setAttribute("user", user);

		return "/userManager";
	}
}

——————————————————————
项目结构:
org.hibernate.HibernateException: No Session found for current thread
——————————————————————
数据库信息:
org.hibernate.HibernateException: No Session found for current thread

org.hibernate.HibernateException: No Session found for current thread
hibernateProperties属性配置加上以下试试:
<prop key=”hibernate.current_session_context_class”>org.springframework.orm.hibernate4.SpringSessionContext</prop>
org.hibernate.HibernateException: No Session found for current thread
引用 1 楼 zhangjihao 的回复:

hibernateProperties属性配置加上以下试试:
<prop key=”hibernate.current_session_context_class”>org.springframework.orm.hibernate4.SpringSessionContext</prop>

org.hibernate.HibernateException: No Session found for current thread
还是会出现这个错误

org.hibernate.HibernateException: No Session found for current thread
引用 2 楼 mdd2012 的回复:
Quote: 引用 1 楼 zhangjihao 的回复:

hibernateProperties属性配置加上以下试试:
<prop key=”hibernate.current_session_context_class”>org.springframework.orm.hibernate4.SpringSessionContext</prop>

org.hibernate.HibernateException: No Session found for current thread
还是会出现这个错误

hibernate4不再需要Spring管理事务了,所以Spring必须放行所有事务(开启)。

org.hibernate.HibernateException: No Session found for current thread
引用 3 楼 zhangjihao 的回复:
Quote: 引用 2 楼 mdd2012 的回复:
Quote: 引用 1 楼 zhangjihao 的回复:

hibernateProperties属性配置加上以下试试:
<prop key=”hibernate.current_session_context_class”>org.springframework.orm.hibernate4.SpringSessionContext</prop>

org.hibernate.HibernateException: No Session found for current thread
还是会出现这个错误

hibernate4不再需要Spring管理事务了,所以Spring必须放行所有事务(开启)。

怎么放行所有事务,具体怎么做?我是新手

org.hibernate.HibernateException: No Session found for current thread
80分
引用 4 楼 mdd2012 的回复:
Quote: 引用 3 楼 zhangjihao 的回复:
Quote: 引用 2 楼 mdd2012 的回复:
Quote: 引用 1 楼 zhangjihao 的回复:

hibernateProperties属性配置加上以下试试:
<prop key=”hibernate.current_session_context_class”>org.springframework.orm.hibernate4.SpringSessionContext</prop>

org.hibernate.HibernateException: No Session found for current thread
还是会出现这个错误

hibernate4不再需要Spring管理事务了,所以Spring必须放行所有事务(开启)。

怎么放行所有事务,具体怎么做?我是新手

就是transactionBese里配置的事务全都放行,如果web层加了OpenSessionViewFilter也要放行。

org.hibernate.HibernateException: No Session found for current thread
web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath*:config/spring-*.xml</param-value>
  </context-param>
  
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:config/spring-*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <filter>
  	<filter-name>encodingFilter</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  	<init-param>
  		<param-name>forceEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  </filter>
  <!-- encoding filter for jsp page -->
  <filter-mapping>
  	<filter-name>encodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <filter>
  	<filter-name>openSession</filter-name>
  	<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>openSession</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>
org.hibernate.HibernateException: No Session found for current thread
引用 5 楼 zhangjihao 的回复:

就是transactionBese里配置的事务全都放行,如果web层加了OpenSessionViewFilter也要放行。

我是用代理进行配置的,没有使用拦截器

org.hibernate.HibernateException: No Session found for current thread
引用 5 楼 zhangjihao 的回复:

就是transactionBese里配置的事务全都放行,如果web层加了OpenSessionViewFilter也要放行。

虽然没有解决问题,但非常感谢你的帮助。

org.hibernate.HibernateException: No Session found for current thread
@Transactional

在你的controller方法上加

例如


@RequestMapping(value="fileupload.do",method={RequestMethod.GET,RequestMethod.POST})
	@Transactional
	@ResponseBody
	public String execute(String fileId,@RequestParam(value = "filenamet", required = false) MultipartFile file,HttpServletRequest request,HttpServletResponse response,ModelMap model){

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明org.hibernate.HibernateException: No Session found for current thread
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!