0

I still don't understand the following:

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;
}

Both xxx and finder.find are under tx control and propagation is required, but I found that only the first modify commits to the db. When I remove the find method, none of the modifies are committed. Why is this?

Ok, here is my code:

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();
}

And xxx method in QuestionairService:

    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;
    }

And here is my configuration

<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>
  • The only thing i can image is: spring flush session, after find method called。 but it doesn't make sense. if i remove evict, the teacher's name has been set to "ff". so, that means session didn't been flushed... i totaly comfused – user2107830 Nov 02 '12 at 07:28

1 Answers1

1

Calling evict() prevents the object from being subsequently persisted. To "re-persist" it, you need to call save() again.

pap
  • 27,064
  • 6
  • 41
  • 46
  • thanks pap, but what i'm concerning is how spring could do that. for example, i use default propogation, then, the transaction used in find should be join to the one used in xxx method. but i evict the po before transaction commit, then why my first modify successful commited? – user2107830 Nov 02 '12 at 09:19
  • Hard to say with the limited code you've provided. It might be easier if you provided at minimum the full method declarations for xxx and `finder.find()`. How you've declared your transaction manager etc in your spring config would also be interesting. – pap Nov 02 '12 at 09:23
  • oh, i found i'm wrong, `flush` doesn't clear the session right? then i think spring flush the seesion after `find`, then after `xxx`,it tells db to commit it. that's why first modify could work. am i right? – user2107830 Nov 02 '12 at 09:36