beijingdongfangdaxue 2012-11-30 17:32
浏览 229
已采纳

Spring2.5MVC和hibernate集成出错,事务问题

 

下面是我spring的配置文件 ,service中我也使用了@Transactional(propagation=Propagation.REQUIRED),配置文件中也配置了,为什么会出现这样的错误啊

 

错误:

org.hibernate.HibernateException: createQuery is not valid without active transaction




spring.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:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-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
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  
  <aop:aspectj-autoproxy proxy-target-class="true"/>
 <!-- 引入properties文件 采用属性占位符的方式 -->
 <context:property-placeholder location="/WEB-INF/config/jdbc.properties"/>
 <!-- hibernate数据源配置 -->
 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  
  <property name="driverClassName" value="${driverClassName}" />
  <property name="url" value="${url}" />
  <property name="username" value="${username}" />
  <property name="password" value="${password}" />
  <!-- 连接池启动时候的初始值 -->
  <property name="initialSize" value="${initialSize}" />
  <!-- 连接池的最大值 -->
  <property name="maxActive" value="${maxActive}"/>
  <!-- 最大空闲时,当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,
   一直减少到maxIdle为止 -->
  <property name="maxIdle" value="${maxIdle}" />
  <!-- 最小空闲值,当空闲的连接数量小于阀门值时,连接池就会申请一些去连接,以免洪峰来时来不及申请 -->
  <property name="minIdle" value="${minIdle}" />
 </bean>
 <!-- 定义SessionFactory(hiberante二级缓存) -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <!-- hibernate的数据源 -->
  <property name="dataSource" ref="dataSource"/>
  
  <!-- hibernate的映射文件 -->
  <property name="mappingResources">
   <list>
    <value>cn/yingtong/bean/Person.hbm.xml</value>
   </list>
  </property>
  
  <!-- 用于配置hibernate的属性信息 -->
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
    
    <!-- 是否使用hibernate的二级缓存 -->
    <prop key="hibernate.cache.use_second_level_cache">true</prop>
    
    <!-- 是否使用查询缓存 -->
    <prop key="hibernate.cache.use_query_cache">false</prop>
    
    <!-- hibernate二级缓存的驱动类 -->
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
   
    <prop key="hibernate.current_session_context_class">thread</prop> 
   
    <prop key="hibernate.transaction.factory_class"> 
        org.hibernate.transaction.JDBCTransactionFactory 
    </prop> 
    
   </props>
   
  </property>
 </bean>
 
 <!-- Spring提供的hibernate事务管理器 -->
 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
 </bean>
 
 <!-- 使用事务注解方式 -->
 <tx:annotation-driven transaction-manager="txManager"/>
 
 
 
 
 
</beans>
如果不加<prop key="hibernate.current_session_context_class">thread</prop>会 出现
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个错误

希望大家给点建议,网上也查了说加上    <prop key="hibernate.current_session_context_class">thread</prop> 就可以了,但是还是不行啊。
麻烦各位给指点一下。
积分不多,都拿出来了。
  • 写回答

2条回答 默认 最新

  • jinnianshilongnian 2012-11-30 18:09
    关注

    如果是hibernate3 可以不加
    thread
    由spring管理事务

    但报如下错误:
    No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个错误

    怀疑你使用方式不对,你应该继承HibernateDaoSupport 然后通过getHibernateTemplate操作,而不应该自己获取sessionFactory然后拿session(如sessionFactory.getcurrentSession)

    可以贴一下出错的详细异常和错误相关代码

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误