分析:no session found for current session原因

本文分析了SSH框架下出现'no session found for current session'错误的原因,包括未开启事务管理器、事务管理器配置错误、Spring事务切入点不正确。并提供了正确的Spring声明式事务配置作为解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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>


如果有任何意见或者更高明的方式,老猫愿闻其详!谢谢!

【下面是本人实际开发时候使用的,转载请尊重劳动成果,谢谢:http://blog.youkuaiyun.com/nthack5730

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值