下面是我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> 就可以了,但是还是不行啊。
麻烦各位给指点一下。 积分不多,都拿出来了。