| 
                         List的size()方法明显是public为何还会出现不可访问的异常。该问题并不是每一次都会出现,经过多次尝试,该异常一直未在测试环境重现。该接口在完整调用链路中的出错次数占总调用次数的比率为0.01%,无意中联想到并发问题在周期性时间内往往是概率性发生。编写模拟多线程环境并发读取公司列表测试代码: 
 
- <mapper namespace="CompanyMapper"> 
 -     <select id="getCompanysByIds"resultType="cn.com.shaobingmm.Company"> 
 -         select * 
 -         from company 
 -         <where> 
 -             <if test="list != null and list.size() > 0"> 
 -                 and id in 
 -        <foreach collection="list" item="id" open="(" separator="," close=")">#{id} 
 - </foreach> 
 -             </if> 
 -         </where> 
 -     </select> 
 - </mapper> 
 
  
多线程并发环境下的压测代码 
- String resource = "mybatis-config.xml"; 
 -         InputStream in = null; 
 -         try { 
 -             in = Resources.getResourceAsStream(resource); 
 -             SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); 
 -             final List<Long> ids = Collections.singletonList(1L); 
 -             final SqlSession session = sqlSessionFactory.openSession(); 
 -             final CountDownLatch mCountDownLatch = new CountDownLatch(1); 
 -             for (int i = 0; i < 50; i++) { 
 -                 Thread thread = new Thread(new Runnable() { 
 -                     public void run() { 
 -                         try { 
 -                             mCountDownLatch.await(); 
 -                         } catch (InterruptedException e) { 
 -                             e.printStackTrace(); 
 -                         } 
 -                         for (int k = 0; k < 100; k++) { 
 -                             session.selectList("CompanyMapper.getCompanysByIds", ids); 
 -                         } 
 -                     } 
 -                 }); 
 -                 thread.start(); 
 -             } 
 -             mCountDownLatch.countDown(); 
 -             synchronized (MybatisBugTest.class) { 
 -                 try { 
 -                     MybatisBugTest.class.wait(); 
 -                 } catch (InterruptedException e) { 
 -                     e.printStackTrace(); 
 -                 } 
 -             } 
 -  
 -         } catch (IOException e) { 
 -             e.printStackTrace(); 
 -         } catch (Throwable e) { 
 -             e.printStackTrace(); 
 -         } finally { 
 -             if (in != null) 
 -                 try { 
 -                     in.close(); 
 -                 } catch (IOException e) { 
 -                     e.printStackTrace(); 
 -                 } 
 -         } 
 
                          (编辑:泰州站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |