0

I have following Thread and transactional method, I have get thrown an exception to test rollback of insertion of DB but notthing is changed. What am I missing?

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

    }

    }

Application Context:

 <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"/>

Using Spring 3.1

Ahmet Karakaya
  • 9,899
  • 23
  • 86
  • 141

1 Answers1

4

You're invoking method doJob() from method run() of the same class. That's why you're working with real method, not proxied one.

Actually, this question was covered in this topic: One Service method invoke inner multiple method for Spring transaction

Community
  • 1
  • 1
Ivan
  • 837
  • 6
  • 15
  • 1
    ..and one solution would be to Autowire yourself and then call myThread.doJob() - http://stackoverflow.com/questions/17279228/how-to-autowire-bean-in-same-bean – Andy Dufresne Dec 09 '14 at 09:12
  • As you can see that I am using prototype scope and which instance will be autowired? – Ahmet Karakaya Dec 09 '14 at 12:42