论坛首页 Java企业应用论坛

我的Spring管理Hibernate事务的配置问题

浏览 9734 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-01-16  
Spring的配置文件如下,配了所谓事务。但是结果不行。
<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
        &lt;property name="sessionFactory"&gt;&lt;ref local="sessionFactory"/&gt;&lt;/property&gt;
    </bean>
    <bean id="txProxyTemplate" abstract="true"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        &lt;property name="transactionManager"&gt;&lt;ref bean="transactionManager"/&gt;&lt;/property&gt;
        &lt;property name="transactionAttributes"&gt;
            &lt;props&gt;
                &lt;prop key="save*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
                &lt;prop key="remove*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
                &lt;prop key="*"&gt;PROPAGATION_REQUIRED,readOnly&lt;/prop&gt;
            &lt;/props&gt;
        &lt;/property&gt;
    </bean>
<bean id="personManager" parent="txProxyTemplate">
        &lt;property name="target"&gt;
            <bean class="com.jsgm.jsqts.equipment.service.impl.PersonManagerImp">
                &lt;property name="personDAO"&gt;&lt;ref bean="personDAO"/&gt;&lt;/property&gt;
            </bean>
        &lt;/property&gt;
        &lt;property name="transactionAttributes"&gt;
            &lt;props&gt;
                &lt;prop key="save*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
                &lt;prop key="remove*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
                &lt;prop key="*"&gt;PROPAGATION_REQUIRED,readOnly&lt;/prop&gt;
            &lt;/props&gt;
        &lt;/property&gt;
    </bean>

此时是不是personManager这个实例所执行的save开头的方法都带有事务特性呢?测试过好像不行。只能在DAO里面这样写,测试出事务的回滚。究竟Spring+Hibernate 怎么做事务呢???非常疑惑。
public void savePersonTest(){
List obj = (List) getHibernateTemplate().execute(
                new HibernateCallback() {
                        public Object doInHibernate(Session session)
                                        throws HibernateException,SQLException {
                        Person person=new Person();
                        person.setIdcard("AAAAAAA");
                        person.setPersonNm("adsfasdf");
                        person.setAreaId("302000");
                        person.setOpStatus("1");
                        person.setUpTime(new Date());
                        person.setExchgFlag("1");
                        person.setMtUnit("3");
                        person.setInsertUnit("3");
                       
                        Person person1=new Person();
                        person1.setIdcard("BBB");
                        person1.setPersonNm("adsfasdf");
                        person1.setAreaId("302000");
                        person1.setOpStatus("1");
                        person1.setUpTime(new Date());
                        person1.setExchgFlag("1");
                        person1.setMtUnit("3");
                        person1.setInsertUnit("3");
                       
                        session.save(person);
                        if (1==1){
//强行抛出错误,让事务回滚
                        throw new SQLException();
                        }
                        session.save(person1);
                        return null;
                        }
                });
}
   发表时间:2005-01-17  
这是我写的一篇文章
http://blog.csdn.net/yzhz/archive/2005/01/17/springqa.aspx
第六个问题的答案可以解答你
0 请登录后投票
   发表时间:2005-01-22  
session.save(person);
session.flush();
0 请登录后投票
   发表时间:2005-02-25  
你的这种事务应用的方法是错误的

1、你把事务管理放到了dao层是个错误,dao层不涉及事务方面的东东,你现在的做法的结果是Spring管理的是你的personDAO的事务,在你的测试用例里Spring是对personDAO的save方法进行事务管理,你添加一个person,如果不抛出runtimeexception,事务就会提交,你调用两次save方法就是两次事务,两者是分开的,当然不会回滚

2、事务管理应该放在服务层或业务层,比如说在服务层有个save方法,这个方法里面可以调用多个dao操作,事务管理应该加在服务层对象上。

3、另外要注意的是:你在调用服务层方法时,注入的参数bean应该是动态代理bean
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics