异常 002 出现异常:connection closed,大佬们求教,是不是多线程影响到了,下面是方法 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Bryant0814
V2EX    Java

异常 002 出现异常:connection closed,大佬们求教,是不是多线程影响到了,下面是方法

  •  
  •   Bryant0814 2024-02-01 16:14:12 +08:00 1496 次点击
    这是一个创建于 621 天前的主题,其中的信息可能已经有所发展或是发生改变。

    @Resource private SqlSessionTemplate sqlSessionTmplate;

    public void threadInsert(List<StudentVO> studentVOList) throws SQLException { long start = System.currentTimeMillis(); Connection cOnnection= sqlSessionTemplate.getConnection(); CacheTestMapper cacheTestMapper = sqlSessionTemplate .getSqlSessionFactory() .openSession() .getMapper(CacheTestMapper.class); try { //设置手动提交 connection.setAutoCommit(false); //先删除数据 cacheTestMapper.deleteStudentById(1L); //获取线程池 ExecutorService executorService = ExecutorUtil.getThreadPool(); //拆分数据创建任务 List<List<StudentVO>> lists = this.averageAssign(studentVOList, 5); Thread[] threads = new Thread[lists.size()]; //监控子线程执行完毕,再执行主线程,要不然会导致主线程关闭,子线程也会随着关闭 CountDownLatch countDownLatch = new CountDownLatch(lists.size()); for (int i = 0; i < lists.size(); i++) { List<StudentVO> list = lists.get(i); threads[i] = new Thread(() -> { try { //批量添加,mybatisPlus 中自带的 batch 方法 cacheTestMapper.batchInsert(list); } finally { countDownLatch.countDown(); } }); } for (int i = 0; i < lists.size(); i++) { executorService.execute(threads[i]); } //当子线程执行完毕时,主线程再往下执行 countDownLatch.await(); System.out.println("添加完毕"); connection.commit(); long end = System.currentTimeMillis(); System.out.println("多线程耗时:" + (end - start)); } catch (Exception e) { connection.rollback(); throw new RuntimeException("002 出现异常:" + e.getMessage()); } finally { connection.close(); } } 
    10 条回复    2024-02-03 13:33:16 +08:00
    MoonWalker
        1
    MoonWalker  
       2024-02-01 16:16:39 +08:00
    finally 里的代码比 Thread 里的代码先运行
    Bryant0814
        2
    Bryant0814  
    OP
       2024-02-01 16:19:36 +08:00
    @MoonWalker 这个 CountDownLatch 不是监控了子线程吗
    cheng6563
        3
    cheng6563  
       2024-02-01 16:26:39 +08:00
    jdbc 不一定是线程安全的
    Bryant0814
        4
    Bryant0814  
    OP
       2024-02-01 16:39:44 +08:00
    @cheng6563 结合我这个说一下
    mango88
        5
    mango88  
       2024-02-01 16:43:36 +08:00
    看上去你的 batchInsert 并没有用到你提前存下的 connection
    mango88
        6
    mango88  
       2024-02-01 17:04:44 +08:00
    还有打印异常最好不要吞掉全部的堆栈信息,根据堆栈报错信息结合源码看 你应该就清楚问题在哪了
    sujin190
        7
    sujin190  
       2024-02-01 17:26:00 +08:00
    不知道你用的数据库是啥,似乎没有数据库支持同一个连接多线程并发事务吧,否则必须每个线程一个连接单独事务你这等待回滚的意义在哪,多线程写同一个连接数据肯定损坏了就可能会出这个 connection closed 异常
    wWjd5V5L0636B5YV
        8
    wWjd5V5L0636B5YV  
       2024-02-02 10:15:59 +08:00
    找到原因了么?我这没发复现啊
    bocharud
        9
    bocharud  
       2024-02-02 10:48:19 +08:00
    如 #5 和 #7 说的那样, cacheTestMapper 变量内部的 connection 没有被带到 子线程中去.

    没用过 mybatis, 不过按照你的代码来看, 你前面先拿出一个 connection 调用 setAutoCommit(false). 是否可以说明 mapper 和内部的 conn 和你这个 conn 相等. 那么可以认为它在类似于 thread_local 里面.

    如果真是这样, 那么子线程的 mapper 获取不到 conn, 所以它说 connection closed
    a5X77vajGRyLA2aF
        10
    a5X77vajGRyLA2aF  
       2024-02-03 13:33:16 +08:00 via Android
    简单看下,存在两个问题
    1. 没有做父子线程事务传递
    2. 没有考虑任务数超出线程池上限

    推测异常原因:
    问题 2 导致一直 await ,直到连接被自动清理线程关闭
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2964 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 39ms UTC 14:09 PVG 22:09 LAX 07:09 JFK 10:09
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86