Spring事务总是报org.hibernate.HibernateException: No Session found for current thread

今天搞了一下spring mvc的注解,弄到数据库的时候,向数据库中存数据没办法存入成功,看一下错误代码是org.hibernate.HibernateException: No Session found for current thread,网上找了一些方法一直没解决,哪位帮忙看一下,代码如下
applicationContext-db.xml代码:

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"
default-lazy-init="true">

<bean id="druid_dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="jdbc:oracle:thin:@localhost:1521:local" />
<property name="username" value="activity" />
<property name="password" value="activitygc" />
<property name="maxActive" value="20" />
<property name="validationQuery" value="select * from dual" />
<property name="testWhileIdle" value="true" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="druid_dataSource" />
<property name="packagesToScan" value="com.mvc.model*"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<!-- <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext
</prop> -->
<!-- <prop key="hibernate.current_session_context_class">thread</prop>  -->
</props>
</property>
</bean>

<!-- 开启AOP监听 只对当前配置文件有效 -->
<aop:aspectj-autoproxy expose-proxy="true" />

<!-- 开启注解事务 只对当前配置文件有效 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="merge*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="put*" propagation="REQUIRED" />
<tx:method name="use*" propagation="REQUIRED" />
<!-- hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 -->
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="count*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" />
<tx:method name="is*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>

<aop:config expose-proxy="true" proxy-target-class="true">
<!-- 只对数据层实施事务 -->
<aop:pointcut id="txPointcut" expression="execution(* com.mvc.dao..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
<!-- 扫描并自动装配 -->
<context:component-scan base-package="com.mvc.dao" />
<context:component-scan base-package="com.mvc.service" />
</beans>

spring-servlet.xml代码
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"
default-lazy-init="true">
<!-- 启用spring mvc 注解 -->
<context:annotation-config />
<!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scan base-package="com.mvc.*" />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">  
        <property name="order" value="0"></property>  
     </bean>
     <!-- 启动 Spring MVC 的注解功能,完成请求和注解 POJO 的映射 -->  
     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
        <property name="messageConverters">  
            <list>  
                 <bean class="org.springframework.http.converter.StringHttpMessageConverter" />  
             </list>  
         </property>  
     </bean>  
    
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/test/" p:suffix=".jsp" />

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8" />
     </beans> 
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_2_5.xsd" id="WebApp_ID" version="2.5"> 
  <display-name>s3h3</display-name> 
   <context-param>   
     <param-name>contextConfigLocation</param-name>   
     <param-value>classpath:applicationContext*.xml</param-value>   
</context-param>   
<listener>   
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
</listener>   
  <context-param>  
        <param-name>log4jConfigLocation</param-name>  
        <param-value>/WEB-INF/log4j.properties</param-value>  
     </context-param>
<servlet>   
     <servlet-name>spring</servlet-name>   
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
     <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring-servlet.xml</param-value>
</init-param>
     <load-on-startup>1</load-on-startup>   
</servlet>  
  <filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.jspx</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.jhtml</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.htm</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<servlet-mapping>   
     <servlet-name>spring</servlet-name>
     <url-pattern>*.do</url-pattern>   
</servlet-mapping>   
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app> 

 

3个回答

这个的问题
参考这个 估计就明白了
[url]http://jinnianshilongnian.iteye.com/blog/1762632[/url]

aihaozhe101
aihaozhe101 可以了,按照您说的方式,之前的是看错了位置,谢谢!
6 年多之前 回复
jinnianshilongnian
jinnianshilongnian <aop:pointcut id="txPointcut" expression="execution(* com.mvc.dao..*.*(..))" /> 你到dao 然后根据 http://jinnianshilongnian.iteye.com/blog/1850432 确认下 代理起作用了
6 年多之前 回复
aihaozhe101
aihaozhe101 <context:component-scan base-package="com.mvc.controller" > <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>我看了一下您推荐的,这样修改了一下,可是还是那个问题,
6 年多之前 回复











这些增删改操作,加上readOnly=false的配置

aihaozhe101
aihaozhe101 没作用,还是那个错误!
6 年多之前 回复

你这里是的事物配置了2份,一份是注解的,一个是利用aop xml方式进行事物管理,你可以去掉一个
这里是注解事物,并且事物transactionManager引用也不对
应该是

下面的
class="org.springframework.orm.hibernate4.HibernateTransactionManager">



tx:attributes














/tx:attributes
/tx:advice



/aop:config
这里是利用aop进行事物代理,这些可以删除
然后参考下http://stackoverflow.com/questions/8846586/no-session-found-for-current-thread-spring-3-1-x-and-hibernate-4
这篇文档进行注解事物配置测试下

jinandmei
jinbingchuan 现在是你事物并没有绑定到dao,不清楚你的代码是如何写的<aop:pointcut id="txPointcut" expression="execution(* com.mvc.dao..*.*(..))" /> 最好放在 service 层
6 年多之前 回复
aihaozhe101
aihaozhe101 我就是想用事务代理的,不用注解的事务,而且删掉那个注解的还是有这么问题
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!