在SSH中,有时候会遇到not session found for current session的问题,刚刚我也遇到一个,现在总结一下:
错误原因:【下面代码有对应序号解析】
1.有没有开启事务管理器,并且把Hibernate的事务提交给Spring管理
2.transactionManager扫描方法名是否正确或者是所扫描的包名下的方法定义是否正确【重点,多错在这里】
3.Spring事务的切入点是否正确
下面是正确的Spring声明式事物配置:
【下面是本人实际开发时候使用的,转载请尊重劳动成果,谢谢:http://blog.youkuaiyun.com/nthack5730】
<!--1. 配置hibernate的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 2.配置事务属性 除了下面定义的方法,其他的方法都不走事务 假如出现not session found for current session这个
错误的话 可以看看是不是调用的方法没有在下面这个开启事务
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<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>
<!-- 3.配置事务切入点,所有Service包括子包都切入,再把事务属性和事务切入点管理起来 -->
<aop:config>
<aop:pointcut expression="execution(* com.eqmt.service.*.*(..))" id="txtPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txtPointcut"/>
</aop:config>