开发者社区> 问答> 正文

使用@Async异步任务提交事物不生效?报错

@Override
    @Async("asyncInvoiceExecutor")
    public void printInvoiceByType(List<InvoiceLog> invoiceLogList, Integer invoiceType, BillTaxControlInfo taxControlInfo) {
        for (InvoiceLog invoiceLog : invoiceLogList) {
            InvoiceLog invoiceLogResult = null;
            if (invoiceType == 1 || invoiceType == 2) {
                invoiceLogResult = commonInvoiceService.paperInvoiceAndPrint(invoiceLog, taxControlInfo);
            } else if (invoiceType == 3) {
                invoiceLogResult = commonInvoiceService.electronicRealTimeInvoice(invoiceLog, taxControlInfo);
            }
            logger.debug("======================= invoice print end...========================="+(invoiceLogResult == null));
            if (invoiceLogResult != null) {
                invoiceLogService.updateByPrimaryKeySelective(invoiceLogResult);
//                updateInvoiceLog(invoiceLogResult);
                logger.debug("======================= update end...=========================");
            }
        }
    }

    @Transactional
    public int updateInvoiceLog(InvoiceLog invoiceLog){
        return invoiceLogMapper.updateByPrimaryKeySelective(invoiceLog);
    }

如上代码所示,@Async首先得用在不同类中,所以下放到Service中的一个方法,这个方法有耗时操作(调用外部接口打印发票),打印完结果还要更新到数据库,为了避免大事物,将方法命名为printInvoiceByType(只读)。后面调用另一个service的方法更新记录结果,可是事物根本没提交,打印结果如下:

[18 14:28:25,176 DEBUG] [asyncInvoiceExecutor-1] rest.RestUtil - =============================调用返回结果:"{\"RetMsg\":\"4001-发票数据不合法 [A632,开票人长度不正确!]\",\"RetCode\":\"4001\"}"
[18 14:28:25,181 INFO ] [asyncInvoiceExecutor-1] impl.CommonInvoiceServiceImpl - 开具发票失败!===================>:4001-发票数据不合法 [A632,开票人长度不正确!]
[18 14:28:25,182 DEBUG] [asyncInvoiceExecutor-1] impl.InvoicingServiceImpl - ======================= invoice print end...=========================false
[18 14:28:25,183 DEBUG] [asyncInvoiceExecutor-1] InvoiceLogMapper.updateByPrimaryKeySelective - ==>  Preparing: update bill_invoice_log SET invoice_status = ?, result_code = ?, result_msg = ? where invoice_id = ? 
[18 14:28:25,183 DEBUG] [asyncInvoiceExecutor-1] InvoiceLogMapper.updateByPrimaryKeySelective - ==> Parameters: 9(Integer), 4001(String), 4001-发票数据不合法 [A632,开票人长度不正确!](String), 200011(Integer)

那个update end...也没打印,不知道怎么回事。事物是通过AOP切面配置的。

Thread dump 结果如下:

"asyncInvoiceExecutor-1@10943" prio=5 tid=0x29 nid=NA waiting
  java.lang.Thread.State: WAITING
	  at sun.misc.Unsafe.park(Unsafe.java:-1)
	  at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
	  at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
	  at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
	  at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
	  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
	  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	  at java.lang.Thread.run(Thread.java:748)

这到底怎么回事,阻塞了吗,也没报错。为什么产生这种现象。

展开
收起
爱吃鱼的程序员 2020-06-08 11:01:13 776 0
0 条回答
写回答
取消 提交回答
问答地址:
问答排行榜
最热
最新

相关电子书

更多
fibjs 模块重构从回调到协程--陈垒 立即下载
fibjs 模块重构从回调到协程 立即下载
低代码开发师(初级)实战教程 立即下载