如何理解PROPAGATION_REQUIRED

时间:2012-11-02 03:25:11

标签: spring transactions hibernate-session

我仍然不明白以下内容:

method xxx{
    Teacher t=dao.get(new Teacher(1));
    t.setName("mark"); 

    finder.find(Teacher.class,null,null,null);

    dao.getSf().getCurrentSession().evict(t);               

    t.setName("ff"); 
    return t;
}

xxxfinder.find都处于tx控制之下并且需要传播,但我发现只有第一次修改才会提交给db。当我删除find方法时,没有提交任何修改。这是为什么?

好的,这是我的代码:

public List<T> find(Class<T> bean, Map<String, Object> param,Pageable pg,Sortable od) {
    //from Questionair t where 1=1
    String hql="from "+this.getBeanName(bean)+" "+this.allias+" where 1=1 "+this.getWhereCourse(param)+" ";
    if(od!=null && od.getDir()!=null && od.getSort()!=null){
        hql+="order by "+this.allias+"."+od.getSort()+" "+od.getDir();
    }
    //开始查询
    Session s=sf.getCurrentSession();
    Query q=sf.getCurrentSession().createQuery(hql);
    if(pg!=null){
        q.setFirstResult(pg.getStart());
        q.setMaxResults(pg.getStart()+pg.getLimit());
    }
    System.out.println(q);
    return q.list();
}

和QuestionairService中的xxx方法:

    try{
        Teacher t=dao.get(new Teacher(1));
        t.setName("mark"); 

        //-------------
        System.out.println("加入finder");
        finder.find(Teacher.class,null,null,null);

        //-------------
        System.out.println("加入evict");
        dao.getSf().getCurrentSession().evict(t);


        t.setName("ff"); 
        return t;
    }

这是我的配置

<tx:advice id="txAdvice" transaction-manager="tm">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED" />
        <!--  <tx:method name="find" propagation="REQUIRES_NEW"/> -->
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:pointcut id="allService" expression="(execution(* com.etc.ssh.service.*.*(..)))" />
    <aop:pointcut id="allFinders" expression="(execution(* com.etc.ssh.finder.*.find(..)))" />
    <aop:pointcut id="allSession" expression="(execution(* *.getCurrentHttpSession(..)))" />
    <aop:advisor pointcut-ref="allService" advice-ref="txAdvice"/>
    <aop:advisor pointcut-ref="allFinders" advice-ref="txAdvice"/>
</aop:config>

<aop:config proxy-target-class="true">
    <aop:advisor pointcut-ref="allSession" advice-ref="ssAdvice"/>
</aop:config>

1 个答案:

答案 0 :(得分:1)

调用evict()可防止对象随后被持久化。要“重新坚持”它,您需要再次致电save()