ITeye博客

Spring 2.5 是不是和 Spring2.0 不兼容啊?

juggler · 2007-12-08 10:50 · 企业架构 · Markdown

原先配置的事务处理现在都不可用了,总是抛出hibernate异常:


javax.servlet.ServletException: org.hibernate.HibernateException: createQuery is not valid without active transaction
	org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
	org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)

我的配置如下: hibernate.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml" />
	</bean>
</beans>

tx.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="load*" read-only="true"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="list*" read-only="true"/>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:advisor
                pointcut="execution(* com.sanlea.shoponline.service.*.*(..))"
                advice-ref="transactionAdvice"/>
    </aop:config>
</beans>

business.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<!-- DAO -->
	<bean id="administratorDao" class="com.sanlea.shoponline.dao.impl.AdministratorDaoImpl">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<!-- Logic -->
	<bean id="administratorLogic" class="com.sanlea.shoponline.logic.impl.AdministratorLogicImpl">
		<property name="administratorDao" ref="administratorDao" />
	</bean>
	
	<!-- Service -->
	<bean id="administratorService" class="com.sanlea.shoponline.service.impl.AdministratorServiceImpl">
		<property name="administratorDao" ref="administratorDao" />
		<property name="administratorLogic" ref="administratorLogic" />
	</bean>
</beans>

评论(14)

  1. #14 楼 · sdh5724 · 2008-10-26 18:41

    最近发现spring2.0.1版本对于 ibatis的集成有严重的编码问题的BUG, 如果你的iBatis的xml配置文件是用非当前系统使用的encoding, 在java6下, 会导致xml文件的读取分析错误。 真是个恶心的东西, spring 用一个文件的 Reader接口把XML配置给了Ibatis, 这个肯定是有问题的。

  2. #13 楼 · saturn · 2008-10-25 21:18

    rock_li writes:

    本人也遇到了同样的问题(spring 2.5.4 + hibernate3.2.5 ga) 网上搜google 大多数都是说 1,pointcut 配置的问题(已检查)。 2)抛出异常不对(抛出RuntimeException 也没有回滚)

    我也碰到过,郁闷

  3. #12 楼 · piggy · 2008-10-17 00:03

    这个问题在下面的链接中有解决方法. http://jira.springframework.org/browse/SPR-4207

  4. #11 楼 · rock_li · 2008-06-30 17:12

    本人也遇到了同样的问题(spring 2.5.4 + hibernate3.2.5 ga) 网上搜google 大多数都是说 1,pointcut 配置的问题(已检查)。 2)抛出异常不对(抛出RuntimeException 也没有回滚)

  5. #10 楼 · EXvision · 2008-02-28 08:48

    marsoon writes:

    LZ你的方法里面用到:sessionFactory.getCurrentSession();了么?

    用了Spring当然使用getHibernateTemplate().xxx了。。不会用到什么sessionFactory吧。

  6. #9 楼 · movingboy · 2008-02-27 22:58

    无用信息,请忽略

  7. #8 楼 · marsoon · 2008-02-27 16:02

    LZ你的方法里面用到:sessionFactory.getCurrentSession();了么?

  8. #7 楼 · juggler · 2007-12-13 17:03

    Hibernate的版本是3.2.5GA

    还是没有解决,真奇怪,没有人遇到这样的问题吗?

  9. #6 楼 · hbrf2007 · 2007-12-13 15:44

    密切关注中.

  10. #5 楼 · andyao · 2007-12-12 11:51

    看看是不是hibernate版本问题 spring2.5需要3.2以上的hibernate版本

  11. #4 楼 · juggler · 2007-12-11 09:43

    antonyup_2006 writes:

    spring2.5对spring2.0是平滑迁移的 一般情况下是不用大动代码!看你的报错应该是事务拦截出错

    不对,Service类是一些包含业务接口的类,我是拦截它的实例方法,应该没错才对。

    在Spring2.0上是可以哦。

  12. #3 楼 · antonyup_2006 · 2007-12-11 09:21

    spring2.5 内置了annotation的包 不象以前还要加个包

  13. #2 楼 · lordhong · 2007-12-10 23:31

    确实有不兼容的地方...transaction, annotation都有问题!

  14. #1 楼 · antonyup_2006 · 2007-12-10 17:16

    spring2.5对spring2.0是平滑迁移的 一般情况下是不用大动代码!看你的报错应该是事务拦截出错

评论功能维护中,当前仅展示历史评论。