spring整合hibernate配置注解事务的相关问题


<bean id="sessionFactory"  
			class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"  
			lazy-init="false">  
			<property name="dataSource">
			<ref bean="dataSource" />
			</property>  
			<property name="mappingResources">  
			<list>  
			<value>com/aaa/healthHouse/model/Cusnation.hbm.xml</value>
			</list>  
			</property>  
			<property name="hibernateProperties">  
			<props>  
			<prop key="hibernate.dialect">  
			org.hibernate.dialect.MySQLDialect
			</prop>  
			<prop key="hibernate.hbm2ddl.auto">update</prop>  
			<prop key="hibernate.show_sql">true</prop>
			<!--<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>  
			--></props>  
			</property>  
			</bean>  

这一段中,hibernate.current_session_context_class这一项不需要配置,当程序出现No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个错误时,可能是因为<tx:annotation-driven transaction-manager="txManager" proxy-target-class="false"/>这段配置中的proxy-target-class中的值为true,即表示代理接口是指定的class,而不是接口代理,当我们在dao层的接口进行@Transaction注解时,会出现这个错误。

当我们将hibernate.current_session_context_class这一项的配置的值设为thread时,即使注解@Trsansaction注解到了接口并且指定代理在类,也能获得session,但是此时获得的session会出现  not actived transaction 即,当前上下文不存在活跃的事务,其实此时可能session不存于spring的管理中,解决的办法:

1、将<tx:annotation-driven transaction-manager="txManager" proxy-target-class="false"/>中的proxy-target-class设为false,,默认就使用接口代理,即默认值即为false

2、@Transaction注解写在dao的实现类层,但是这样违背了我们面向接口编程的思想,不推荐

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值