环境:
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