下面是applicationContext.xml的配置文件:
<!-- Bean代理事务 -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Dao</value>
<value>*Service</value>
<value>*ServiceImpl</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
加入了事物控制的service方法:
public void savePayMentBillCode(List<PaymentExt> paymentExtList) throws ServiceException {
try {
for(PaymentExt paymentExt:paymentExtList) {
paymentExt.setDataStatus(BusinessConstant.ExternalDataStatus.SAVE);
paymentExtDao.save(paymentExt);
}
//创建另一个线程去执行复杂的保存操作。
new Thread(new PaymentThreadService(paymentExtList,paymentDataService)).start();
} catch (Exception e) {
e.printStackTrace();
logger.info(e.getMessage());
throw new ServiceException("保存报文信息异常!" + e.getMessage());
}
}
线程的run方法:
public class PaymentThreadService extends AbstractService implements Runnable{
private List<PaymentExt> paymentExtList;
private PaymentDataService paymentDataService;
public PaymentThreadService(){}
public PaymentThreadService(List<PaymentExt> paymentExtList,PaymentDataService paymentDataService) {
this.paymentExtList = paymentExtList;
this.paymentDataService = paymentDataService;
}
@Override
public void run() {
try {
for (PaymentExt paymentExt : paymentExtList) {
paymentDataService.outerSetlBizType(paymentExt,null);
}
} catch (Exception e) {
e.printStackTrace();
logger.info(e.getMessage());
throw new ServiceException("多线程-保存报文信息异常!" + e.getMessage());
}
}
}
paymentDataService.outerSetlBizType(paymentExt,null);
这段代码中有很多保存操作。
当run方法运行结束后,这些保存操作会被事物提交吗?我在测试时会出现:row was updated or deleted by another transaction (or unsaved-value mapping was incorrect);错误。
好像run方法结束会并没有把session自动结束,还管理着保存的对象,导致其他方法试图获取这些对象时出现上面的异常。
求教谢谢!