zhouxiuliang
zhouxiuliang
采纳率0%
2010-10-18 12:16

关于spring事务管理的问题

已采纳

最近刚学spring,在事务管理这自己写了个小例子,可是老运行不通过,请大虾看看。下面是我的代码:
首先是DAO接口,UserDaoService:
package org.ultimania.dao;

import java.util.List;
import org.ultimania.model.User;

public interface UserDaoService {
public List getAll();
}
接口实现,UserDaoImpl:
package org.ultimania.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.ultimania.model.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDaoService{
@SuppressWarnings("unchecked")
@Transactional(propagation=Propagation.REQUIRED)
public List getAll(){
List list = this.getHibernateTemplate().find("from User");
return list;
}

}
接下来是我的测试代码:
package org.ultimania.test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.ultimania.dao.UserDaoImpl;
import org.ultimania.dao.UserDaoService;
import org.ultimania.model.User;

public class UserDaoTest {

public static void main(String[] args){
    List<User> list = null;
    UserDaoService userDaoImpl ;
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    userDaoImpl = (UserDaoService)ctx.getBean("userDao");
    try{
        list = userDaoImpl.getAll();
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        if(list==null){
            System.out.println("faled");
        }else{
            System.out.println(list.size());
        }
    }
}

}
然后是spring配置文件,applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation"
        value="classpath:hibernate.cfg.xml">
    </property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property>
</bean>

<bean id="userDao" class="org.ultimania.dao.UserDaoImpl">
    <property name="hibernateTemplate">
        <ref bean="hibernateTemplate" />
    </property>
</bean>


我用的是注解式事务管理的方式,hibernate的东西是没错的,现在的问题是如果我在UserDaoImpl不添加事务声明@Transactional(propagation=Propagation.REQUIRED),运行是没有错误的,可是我一添加这句声明,运行就错了。下面是异常信息:
Exception in thread "main"
java.lang.AbstractMethodError: org.springframework.orm.hibernate3.SpringTransactionFactory.beginTransaction(Lorg/hibernate/jdbc/JDBCContext;Lorg/hibernate/transaction/TransactionFactory$Context;)Lorg/hibernate/Transaction;
at org.hibernate.jdbc.JDBCContext.beginTransaction(JDBCContext.java:271)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1079)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:558)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:374)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:263)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:101)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy5.getAll(Unknown Source)
at org.ultimania.test.UserDaoTest.main(UserDaoTest.java:19)
这个问题困扰我好久了,一直没有解决,只能请大虾们帮我看看了。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • oyj7454103 oyj7454103 11年前

    [quote]@Transactional(propagation=Propagation.REQUIRED),运行是没有错误的,可是我一添加这句声明,运行就错了[/quote]

    相应的jar 包 是否加入?
    annotation 的版本和hibernate 版本是否匹配?

    点赞 评论 复制链接分享
  • soartju soartju 11年前





    确定是[color=red]org.ultimania.dao.UserDaoImpl[/color]"?

    UserDaoService userDaoImpl; 你先声明成

    UserDaoImpl userDaoImpl;试试

    点赞 评论 复制链接分享
  • soartju soartju 11年前

    UserDaoService userDaoImpl ;
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    userDaoImpl = ([color=red]UserDaoService[/color])ctx.getBean("userDao");

    改成

    userDaoImpl = ([color=red]UserDaoImpl[/color])ctx.getBean("userDao");

    试一下。

    点赞 评论 复制链接分享
  • enet_java enet_java 11年前

    查询的动作加上这个propagation=Propagation.REQUIRED 肯定不正确呀!!

    应该是readOnly 吧。。

    点赞 评论 复制链接分享

相关推荐