当抛出异常时如何回滚Spring事务

6
我正在使用Spring 3.0.5和Hibernate 3.6。在我的项目中,有这样一种情况:如果任何异常被抛出或发生错误,则必须回滚事务。以下是示例代码,一切都正常运行,但是如果我抛出一个异常,事务不会回滚,但是如果抛出任何异常,例如mysql.IntegrityConstraintException,那么事务将被回滚。为什么在我的情况下没有发生这种情况? applicationContext.xml:
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:database.properties"/>
    </bean>
      <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.driverClassName}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />

        </bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
 <property name="dataSource" ref="myDataSource" />
    <property name="packagesToScan" value="com.alw.imps"/>
    <property name="configLocation">    
        <value>
            classpath:hibernate.cfg.xml
        </value>
     </property>
    </bean>

    <bean id="stateDao" class="com.alw.imps.dao.StateDaoImpl">
     <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>


        <bean id="stateService" class="com.alw.imps.services.StateService">
       <property name="stateDao" ref="stateDao"></property>
       <property name="cityDao" ref="cityDao"></property>
       <property name="customerDao" ref="customerDao"></property>
       </bean>  

        <bean id="customerDao" class="com.alw.imps.dao.CustomerDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
       </bean> 

            <bean id="cityDao" class="com.alw.imps.dao.CityDaoImpl">
              <property name="sessionFactory" ref="sessionFactory"></property>
            </bean>  





<tx:annotation-driven transaction-manager="transactionManager"  />

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>        

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

服务类 StateService

@Transactional(rollbackFor={Exception.class})
public class StateService {

  private StateDaoImpl stateDao;
  private CityDao cityDao;
  private CustomerDao customerDao;

  public void setCustomerDao(CustomerDao customerDao) {
    this.customerDao = customerDao;
  }

  public void setStateDao(StateDaoImpl stateDao) {
    this.stateDao = stateDao;
  }

  public CityDao getCityDao() {
    return cityDao;
  }

  public void setCityDao(CityDao cityDao) {
    this.cityDao = cityDao;
  }

  public void addState() {
    try {
      State state=new State();
      state.setStateName("Delhi");
      stateDao.create(state);
      addCity();
      addCustomer();
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  public void addCity() throws Exception {
    City city=new City();
    city.setCiytName("Delhi");
    city.setStateId(1);
    cityDao.create(city);
  }

  public void addCustomer() throws Exception {
    throw new java.lang.Exception();
  }

DAO

public class StateDaoImpl extends GenericDaoImpl<State, Integer> implements StateDao {
}

GenericDaoImpl

public class GenericDaoImpl<T,PK extends Serializable> implements GenericDao<T,PK> {
  public SessionFactory sessionFactory;
  public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
  }

  public Session getSession() {
    return sessionFactory.getCurrentSession();
  }

  public PK create(T o) {
    Session ss= getSession();
    ss.save(o);
    return null;
  }

hibernate.cfg

<hibernate-configuration>
  <session-factory>
    <property name="connection.pool_size">1</property>
    <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    <property name="defaultAutoCommit">false</property>
    <mapping class="com.alw.imps.pojo.State"/>
    <mapping class="com.alw.imps.pojo.City"/> 
  </session-factory>
</hibernate-configuration>

所以,我说的问题是,当我从addCustomer()方法中抛出Exception类型的异常时,事务没有回滚。

到目前为止一切看起来都很好,应该会回滚事务。有可能事务在调用堆栈的更高层开始。也许如果您添加配置事务的Spring配置,会有所帮助。 - Manuel Quinones
我不相信你的 MyService 类被代理了。你看到的回滚是来自数据库,而不是事务。 - Sotirios Delimanolis
请发布您的配置和带有方法签名/注释的调用堆栈,以便我们更好地评估情况。 - fpmoles
@Moles-JWS,我已经添加了配置和其他依赖项,请检查更新。 - arvin_codeHunk
如果出现mysql的IntegrityConstraintsException,则事务会成功回滚,但是当我抛出类型为Exception的异常时,事务不会回滚。 - arvin_codeHunk
显示剩余2条评论
4个回答

15

您的交易未回滚,因为没有抛出异常:您调用的addState()方法捕获了该异常:

public void addState() {
    try {
        State state=new State();
        state.setStateName("Delhi");
        stateDao.create(state);
        addCity();
        addCustomer();
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

因此,事务性的Spring代理没有看到抛出的任何异常,也没有回滚事务。

对于从DAO抛出的异常,它起作用是因为DAO本身是事务性的,所以它自己的事务性代理检测到DAO抛出的异常并标记事务为回滚。然后将异常传播到服务并被您的代码捕获,但此时事务已经被标记为回滚。


谢谢jb,这是我的错...这是一个愚蠢的错误,不能被接受。 - arvin_codeHunk

5
您的交易没有被回滚,因为您没有让异常传达给Spring框架,而是在您的代码中捕获了异常。 因此,请不要捕获异常,让其传递到Spring框架中。

public void addState() 
{
        try
        {
        State state=new State();
        state.setStateName("Delhi");
        stateDao.create(state);
        addCity();
        addCustomer();
        }
        catch(Exception e)
        {

            e.printStackTrace();
        }
}

使用

public void addState() 
{
        State state=new State();
        state.setStateName("Delhi");
        stateDao.create(state);
        addCity();
        addCustomer();
}

1
交易没有被回滚,因为您自己通过编写catch块捕获了异常。在通常情况下,这是可行的,但在Spring事务中,如果这样做,Spring事务管理器如何知道发生了异常...这就是它没有被回滚的原因。

0

您可以在Spring API文档中找到大多数问题的答案。 @Transactional有一个字段Class<? extends Throwable>[] rollbackFor():

默认情况下,事务将在RuntimeExceptionError上回滚,但不会在已检查的异常(业务异常)上回滚。有关详细说明,请参见org.springframework.transaction.interceptor.DefaultTransactionAttribute.rollbackOn(Throwable)

这意味着对于以下情况,无论调用者如何处理异常,只有第一个RunTimeException情况会默认调用回滚。

// Only this case would roll back by default
@Override
@Transactional
public void testRollbackRuntimeException() {
    // jpa operation.
    throw new RuntimeException("test exception");
}

// never roll back, because its caught.
@Override
@Transactional
public void testRollbackRuntimeExceptionCaught() {
    try {
        throw new RuntimeException("test exception");
    } catch(Exception e) {}
}

// @Transactional(rollbackFor = Exception.class) would also rollback. but by default no
@Override
@Transactional
public void  testRollBackWithExceptionCaught() throws Exception {
    throw new Exception("test exception");
}

// never roll back because the checked exception is caught.
@Override
@Transactional
public void  testRollBackWithExceptionCaught() {
    try {
        throw new Exception("test exception");
    } catch (Exception e) {}
}

大多数情况下,您可能希望无差别地回滚已检查的异常,使用@Transactional(rollbackFor = Exception.class)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接