当前位置: 代码迷 >> J2EE >> Spring注解形式的事务配置,事务不起作用
  详细解决方案

Spring注解形式的事务配置,事务不起作用

热度:473   发布时间:2016-04-17 23:51:09.0
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);
 }
 
 }
 }
这个地方加上  session.beginTransaction();
就是这样
public class Chcb_xjjhBizBean implements IChcb_xjjhBiz{
  public void saveProblemType(DProblemType entity) throws CustomException {
  try{
  session.beginTransaction();
  session.save(entity);
  session.flush();
  } catch(HibernateException se){
  throw ExceptionTranslator.translator(se);
  }
  
  }
  }
这样试试吧 你都没有开启事务怎么生效啊
  相关解决方案