严重: 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) ———————————————————————————————————————– <?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> ———————————————————————————————————————– <?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> ———————————————————————————————————————– <?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> ———————————————————————————————————————– <?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> ———————————————————————————————————————– <?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&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> ———————————————————————————————————————– 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"; } } —————————————————————— |
|
hibernateProperties属性配置加上以下试试:
<prop key=”hibernate.current_session_context_class”>org.springframework.orm.hibernate4.SpringSessionContext</prop> |
|
|
|
hibernate4不再需要Spring管理事务了,所以Spring必须放行所有事务(开启)。 |
|
怎么放行所有事务,具体怎么做?我是新手 |
|
80分 |
就是transactionBese里配置的事务全都放行,如果web层加了OpenSessionViewFilter也要放行。 |
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> |
|
我是用代理进行配置的,没有使用拦截器 |
|
虽然没有解决问题,但非常感谢你的帮助。 |
|
@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){ |