Spring Transactional Rollback不起作用

时间:2014-12-09 08:56:33

标签: java spring

我有跟随Thread和事务方法,我已经抛出异常来测试DB插入的回滚但是没有改变。我错过了什么?

public class CleaningThread extends Thread {

   public void run() {
        try {
            doJob();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

     @Transactional(rollbackFor=Exception.class)
    private void doJob() throws Exception {

    //INSERT OPERATION
     final BatchSqlUpdate bs = new BatchSqlUpdate
     bs.flush()

     throw new Exception("Custom exception")

    //UPDATE

    }

    }

申请背景:

 <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>file:conf/offclear.properties</value>
        </list>
    </property>
</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
</bean>

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

<bean id="cleaningThread" class="CleaningThread" scope="prototype"/>

使用Spring 3.1

1 个答案:

答案 0 :(得分:4)

您正在从同一个类的方法run()调用方法doJob()。这就是你使用真实方法而不是代理方法的原因。

实际上,本主题涵盖了这个问题:One Service method invoke inner multiple method for Spring transaction