【问题标题】:Spring Transactional Rollback does not workSpring事务回滚不起作用
【发布时间】:2014-12-09 08:56:32
【问题描述】:

我有以下线程和事务方法,我抛出了一个异常来测试数据库插入的回滚,但没有任何改变。我错过了什么?

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

【问题讨论】:

    标签: java spring


    【解决方案1】:

    您正在从同一类的方法 run() 调用方法 doJob()。这就是为什么你使用真正的方法,而不是代理方法。

    其实这个问题已经在这个话题中讨论过了:One Service method invoke inner multiple method for Spring transaction

    【讨论】:

    • ..一种解决方案是自己自动装配,然后调用 myThread.doJob() - stackoverflow.com/questions/17279228/…
    • 如您所见,我使用的是原型作用域,哪个实例将被自动装配?
    猜你喜欢
    • 1970-01-01
    • 2018-12-19
    • 2013-04-19
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 2020-08-06
    相关资源
    最近更新 更多