Java Spring事务回滚不起作用
我有下面的线程和事务方法,我抛出了一个异常来测试DB插入的回滚,但并没有改变任何东西。我错过了什么Java Spring事务回滚不起作用,java,spring,Java,Spring,我有下面的线程和事务方法,我抛出了一个异常来测试DB插入的回滚,但并没有改变任何东西。我错过了什么 public class CleaningThread extends Thread { public void run() { try { doJob(); } catch (Exception e) { e.printStackTrace(); } } @Transact
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"/>
文件:conf/offclear.properties
使用Spring3.1,您正在从同一类的方法run()调用方法doJob()。这就是为什么您使用的是实方法,而不是代理方法
实际上,这个问题在本主题中已经讨论过:您正在从同一类的方法run()调用方法doJob()。这就是为什么您使用的是实方法,而不是代理方法
实际上,这个问题在本主题中已经讨论过:。一个解决方案是自动连线,然后调用myThread.doJob()-正如您所看到的,我使用的是prototype作用域,哪个实例将自动连线?。一个解决方案是自动连线,然后调用myThread.doJob()-如您所见,我正在使用prototype scope,哪个实例将自动连接?