日期:2014-05-19  浏览次数:20657 次

Spring注解方式的事务配置,事务不起作用
我使用了注解方式进行事务配置,但是当初错的时候,并没有rollback,这是怎么回事?
具体代码如下:
配置文件:
  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>

接口类:
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public interface IChcb_xjjhBiz{

@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public void saveProblemType(DProblemType entity) throws CustomException;

Servic:
public class Chcb_xjjhBizBean implements IChcb_xjjhBiz{
public void saveProblemType(DProblemType entity) throws CustomException {
try{
session.save(entity);
session.flush();
} catch(HibernateException se){
throw ExceptionTranslator.translator(se);
}

}
}

action:
public String Insert() throws CustomException {

//获取biz bean
IChcb_xjjhBiz isb = (IChcb_xjjhBiz)BizUtil.getBizBean("zyxc.xjjh");

isb.saveProblemType(entity);
isb.saveProblemType(entity);

//返回成功标志
return SUCCESS;

}

这里action调了2次新增方法做测试,第二次的时候主键冲突,但是事务没有回滚,第一次的数据插入到了数据库,这个怎么回事?还请高手指点

------解决方案--------------------
session的状态转换问题。
------解决方案--------------------
不知道抛出的异常是不是RuntimeException,加一个rollbackFor=Exception.class试试
------解决方案--------------------
探讨
public class Chcb_xjjhBizBean implements IChcb_xjjhBiz{
public void saveProblemType(DProblemType entity) throws CustomException {
try{
session.save(entity);
session.flush();
} catch(HibernateException se){
throw ExceptionTranslator.translator(se);
}

}
}