spring事务中的线程 - 并发异常

时间:2016-12-01 10:07:23

标签: java spring multithreading transactions spring-transactions

我可以在Spring @Transactional中使用多个线程吗? 通过这种方式?:

@Async
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public Future<Result> chrgAndSaveLsk(RequestConfig reqConfig, Integer lsk) throws ErrorWhileChrg {
    ChrgServ chrgServ = ctx.getBean(ChrgServ.class);
    //Test threads
    Result res = chrgServ.chrgLsk(calc); <--here working threads!

...

ChrgServ:

@Service
@Scope("prototype")
public class ChrgServ {
//Threads invoking...
public Result chrgLsk(Calc calc) throws ErrorWhileChrg {
...

现在我得到下一个例外:

15:49:16.143 [SimpleAsyncTaskExecutor-2] INFO  com.ric.bill.ChrgServ - ChangeID=1121
java.util.concurrent.ExecutionException: java.lang.NullPointerException
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:192)
    at com.ric.bill.ChrgServ.chrgLsk(ChrgServ.java:235)
    at com.ric.bill.ChrgServThr.chrgAndSaveLsk(ChrgServThr.java:89)
    at com.ric.bill.ChrgServThr$$FastClassBySpringCGLIB$$8f447d36.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)

但是当我删除线程(重写代码非线程)或者在线程处理后将@Transactional移动到该位置时,我的应用程序运行良好!

为什么会这样?我应该对事务中的线程使用什么方法?

0 个答案:

没有答案