zzzzzqiu
2009-07-29 17:34
浏览 199
已采纳

spring+hibernate的事务回滚

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED"/>
        <tx:method name="find*" read-only="true"/>
    </tx:attributes>
</tx:advice>


<!-- applicationContext.xml哪些类的哪些方法参与事务 -->
<aop:config>
    <aop:pointcut id="allServiceMethod" expression="execution(* com.szhtp.wiki.service.impl.*.*(..))"/>
    <aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice"/>
</aop:config>

//以下是com.szhtp.wiki.service.impl.WebAddMeetingServiceImpl.java,是一个webservice
@Service
public class WebAddMeetingServiceImpl implements WebAddMeetingService {
private SimpleHibernateTemplate meetingDao;
private SimpleHibernateTemplate personDao;

@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {

    meetingDao = new SimpleHibernateTemplate<T_ComPhone_Meeting, String>(
            sessionFactory, T_ComPhone_Meeting.class);
    personDao = new SimpleHibernateTemplate<T_ComPhone_Person, String>(
            sessionFactory, T_ComPhone_Person.class);
}
public String addMeeting(String alterdesc,String callername,String callerphone,String callmode,String calltime,
        String centrexid,String department,String hurrytype,String ternaldesc,String ternalday,String inputman,
        String meetaddress,String meetman,String meetname,String meettime,String remark) {                          T_ComPhone_Meeting tcm=new T_ComPhone_Meeting();  
                                tcm.setPersonname(meetmanN);
            meetingDao.saveService(tcm);

        try {List listtel = meetingDao.find(hql1);
        String hql2 = "select id from T_ComPhone_Member where id="+ allperson[i];
            T_ComPhone_Meeting tcm=new T_ComPhone_Meeting();                        personDao.saveService(person);
                    }
                }
            }
            // TODO Auto-generated method stub
  System.out.println("成功添加会议"+"id:"+tcm.getMeetid()+"会议名:"+meetname);
            return "success";

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("会议添加失败");
            return "error";

        }
    }
    return "error";

}

如果在meetingDao.saveService(tcm)之后发生异常,tcm还是会插入到数据库中,怎样才能使他错误回滚呢?
[b]问题补充:[/b]
是只要在方法中catch到运行时异常才回滚,还是要使用throw抛出运行时异常才能回滚?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 我不是逗B 2009-07-31 10:39
    已采纳

    问题补充:
    是只要在方法中catch到运行时异常才回滚,还是要使用throw抛出运行时异常才能回滚?

    如果你是在service这层作的事务,那些是要throw才能回滚。如果你把它catch了,拦截器是抓不到异常的。就不会回滚。

    点赞 评论
  • zhoujuan520 2009-07-29 17:36

    要产生运行时异常才能回滚

    点赞 评论
  • 我不是逗B 2009-07-30 22:08

    要RuntimeException才能回滚

    点赞 评论

相关推荐 更多相似问题