Spring hibernate整合No Session found for current thread错误

 

环境:

pom.xml文件

<properties>
	<spring.version>3.2.0.RELEASE</spring.version>
	<hibernate.version>4.2.6.Final</hibernate.version>
</properties>

 配置事务时,出现org.hibernate.HibernateException: No Session found for current thread

org.hibernate.HibernateException: No Session found for current thread
	at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
	at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:990)
	at com.myssh.dao.BaseDao.getSession(BaseDao.java:392)
	at com.myssh.dao.BaseDao.save(BaseDao.java:62)
................

spring.xml的配置。

    <!-- 配置Hibernate事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> -->
	<!-- <aop:aspectj-autoproxy expose-proxy="true" /> -->
    <!-- 配置事务属性 -->
	 <tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true"/>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="find*" read-only="true"/>  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="do*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="select*" propagation="REQUIRED" />  
            <tx:method name="*" propagation="SUPPORTS" read-only="true"/> 
		</tx:attributes>
	</tx:advice> 
	
	<!-- 配置事务切点,并把切点和事务属性关联起来 -->
    <aop:config proxy-target-class="true">
        <aop:pointcut expression="execution(* com.myssh.service.impl.*.*(..))" id="txPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

如果启用下面代码,就不会报错;

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

网上有很多都是上面这种解决方法,但也有提出疑问的。

事务的实现已经是声明式的了, 为啥还要采用XML配置式的呢?

如果用的是基于基于AspectJ的xml配置事务的话,最有可能出错的地方就是在expression表达式这块:

<aop:pointcut expression="execution(* com.myssh.service.impl.*.*(..))" id="txPointcut"/>

总体代码:

<!--1、基于注解事务-->
<!-- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> -->
<!-- <aop:aspectj-autoproxy expose-proxy="true" /> -->

<!--2、基于声明式事务-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true"/>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="find*" read-only="true"/>  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="do*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="select*" propagation="REQUIRED" />  
            <tx:method name="*" propagation="SUPPORTS" read-only="true"/> 
		</tx:attributes>
	</tx:advice> 	
	<!-- 配置事务切点,并把切点和事务属性关联起来 -->
    <aop:config proxy-target-class="true">
        <aop:pointcut expression="execution(* com.myssh.service.*.*(..))" id="txPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

至于原因嘛,待深入研究后再贴出来。。

网上参考文章

Spring + Hibernate : No Session found for current thread

http://www.mkyong.com/spring/spring-hibernate-no-session-found-for-current-thread/

Spring的4种事务管理(1种编程式事务+三种声明事务)

http://blog.csdn.net/sinat_25926481/article/details/48208619

基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。

http://sishuok.com/forum/blogPost/list/3845.html

 

转载于:https://my.oschina.net/u/737121/blog/758250

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值