0 0

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

今天搞了一下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> 

 

2013年4月23日 15:00

3个答案 按时间排序 按投票排序

0 0

采纳的答案

<context:component-scan base-package="com.mvc.*" />  这个的问题
参考这个 估计就明白了
http://jinnianshilongnian.iteye.com/blog/1762632

2013年4月23日 16:03
0 0

<tx:annotation-driven transaction-manager="transactionManager" />




你这里是的事物配置了2份,一份是注解的,一个是利用aop xml方式进行事物管理,你可以去掉一个
<tx:annotation-driven transaction-manager="transactionManager" /> 这里是注解事物,并且事物transactionManager引用也不对
应该是
<tx:annotation-driven transaction-manager="txManager" />
下面的
<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>
这里是利用aop进行事物代理,这些可以删除
然后参考下http://stackoverflow.com/questions/8846586/no-session-found-for-current-thread-spring-3-1-x-and-hibernate-4
这篇文档进行注解事物配置测试下

2013年4月23日 15:47
0 0

<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" />
这些增删改操作,加上readOnly=false的配置

2013年4月23日 15:46

相关推荐

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

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

    解决SpringDataJPA报错:org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null w

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/...

    Spring4.0+Hibernate4.0+Struts2.3整合案例

    2、报错:org.hibernate.HibernateException: No Session found for current thread 意思是必须在transcation.isActive()条件下才能执行, 可以解决办法是:当方法不需要事务支持的时候,使用 Session ...

    HIbernate4.3.6整合c3p0所需jar

    Caused by: org.hibernate.HibernateException: Could not instantiate connection provider [org.hibernate.connection.C3P0ConnectionProvider] at org.hibernate.engine.jdbc.connections.internal....

    spring_MVC源码

    18. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; 19. &lt;property name="dataSource" ref="dataSource" /&gt; 20. &lt;property name="hibernateProperties"&gt; 21. &lt;props&gt; 22...

    jdbc+hibernate的jar包

    3. `org.hibernate.HibernateException`:Hibernate抛出的异常类。 4. `org.hibernate.Query`和`org.hibernate Criteria`:用于执行HQL(Hibernate Query Language)和Criteria API查询,比直接使用SQL更加灵活。 ...

    com.microsoft.sqlserver.jdbc.SQLServerException: 只进结果集不支持请求的操作 解决方案

    这可以通过在Hibernate配置文件中设置`hibernate.connection.autocommit`属性为`false`,并在查询前手动设置事务,然后通过`Session`的`createSQLQuery()`或`createQuery()`方法附加`ResultSet.TYPE_SCROLL_...

    Hibernate+Proxool配置

    由于项目需求的需要,我们引入了连接池。...我们采用了Hibernate,所以可以考虑hibernate自带的连接池机制,但是发现效率不高,而且Hibernate也推荐使用c3p0或Proxool连接池,在我们的项目中采用了Proxool

    Hibernate配置常见错误

    错误表现:在Session关闭后尝试访问懒加载属性,抛出“org.hibernate.LazyInitializationException: could not initialize proxy - no Session”异常。 解决方案:理解并合理使用Open Session in View(OSIV)模式...

    Spring/泛型Hibernate的实现

    接下来,我们需要实现上述定义的泛型接口,并结合Spring框架来管理Hibernate的Session生命周期和事务处理。具体实现如下: ```java public class HibernateDaoImpl, PK extends Serializable&gt; implements ...

    J2EE利用Hibernate采用B/S架构网页设计

    import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Configuration; /** * Configures and provides access to Hibernate sessions, tied to the * current ...

    hibernate测试时遇到的几个异常及解决方法汇总

    3. org.hibernate.HibernateException: No CurrentSessionContext configured! 该异常的解决方法是添加 hibernate-jpa-2.0-api-1.0.0.Final.jar。这是因为 Hibernate 需要配置 CurrentSessionContext,以便正确地...

    struts2+spring+hibernate

    import org.hibernate.HibernateException; import org.hibernate.LockMode; import org.hibernate.Query; import org.hibernate.Session; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import...

    学习hibernate第一章内容

    在IT行业中,SSH(Struts、Spring、Hibernate)是一个经典的Java Web开发框架组合,其中Hibernate是专注于对象关系映射(ORM)的框架。本章主要介绍Hibernate的基本概念、工作原理以及如何在实际项目中应用。 一、...

    JAVA错误文档.pdf

    4. Hibernate框架错误:文档还包含了“org.hibernate.HibernateException: No Hibernate Session bound to thread”等错误信息,这是Hibernate框架常见的异常,表明在当前线程上没有绑定Hibernate Session。...

    hibernate错误汇总

    错误八:Exception in thread "main" org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: org.model.User 错误原因:这个错误表示在尝试获取主键为1的实体...

    Hibernate开发必备版

    import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; ``` 2. **类声明:** ```java public class HibernateUtil { ``` 3. **静态...

    Hibernate源代码分析

    public org.hibernate.classic.Session getCurrentSession() throws HibernateException { if (currentSessionContext == null) { throw new HibernateException("No CurrentSessionContext configured!"); } ...

Global site tag (gtag.js) - Google Analytics