Example #1
0
  //	@Test
  @Ignore
  public void testThread() throws InterruptedException {
    IUserManagedPool pool = super.getPool();
    MyThread[] threads = new MyThread[CONN_LOOPS];
    for (int j = 0; j < CONN_LOOPS; j++) {
      threads[j] = new MyThread(pool, j, true, count);
    }
    for (int j = 0; j < CONN_LOOPS; j++) {
      threads[j].start();
    }

    while (count.get() < CONN_LOOPS && countException.get() == 0) {
      // 实时监控连接池的状态
      String msg = "当前连接池数量:" + pool.getStatus();
      ConnPrintOutUtil.print(log, ConnPrintOutUtil.INFO, msg);
      ThreadUtils.doWait(super.getPool());
    }
    Thread.sleep(1000);
    // 如果结果不抛出异常 则说明case正确
    if (countException.get() == 0) {
      ConnPrintOutUtil.printSuccess(log);
    } else {
      ConnPrintOutUtil.printFailure(log);
    }
  }
Example #2
0
 @Before
 public void prepare() throws SQLException {
   //		 TODO Auto-generated method stub
   String url = ConnDBConfigUtil.getStringValue("url");
   String uname = ConnDBConfigUtil.getStringValue("uname");
   String pwd = ConnDBConfigUtil.getStringValue("pwd");
   int POOL_SIZE = ConnDBConfigUtil.getIntValue("pool.size");
   super.prepare(url, uname, pwd, POOL_SIZE);
   ConnPrintOutUtil.print(log, ConnPrintOutUtil.INFO, super.getPool().toString());
 }
Example #3
0
 public void consultTable() throws SQLException {
   String sql = "select count(*) from DEPT";
   IConnection conn = pool.poll();
   PreparedStatement ps = conn.prepareStatement(sql);
   ResultSet rs = ps.executeQuery();
   if (rs.next()) {
     if (ConnPrintOutUtil.isOnServer()) {
       Assert.assertEquals(4, rs.getInt(1));
     }
   }
 }
Example #4
0
    @Override
    public void run() {
      // TODO Auto-generated method stub
      try {

        for (int i = 0; i < 100; i++) {
          // 获取连接
          //					pool.getConnection(index);
          IConnection conn = pool.poll();

          // 进行休眠
          int t = rd.nextInt(50) + 1000;
          Thread.sleep(t);

          // 查询表并且释放连接
          consultTable();
          if (isRelease) {
            conn.close();
          }
          Thread.sleep(1 * 1000);
          if (i % 10 == 0) {
            ThreadUtils.doNotify(pool);
          }
        }

      } catch (SQLException e) {
        // TODO Auto-generated catch blockx
        String msg = "thread " + index + " has exception";
        ConnPrintOutUtil.print(log, ConnPrintOutUtil.WARN, msg);
        countException.incrementAndGet();
        count.incrementAndGet();
        ThreadUtils.doNotify(pool);
      } catch (Exception e) {
        // TODO: handle exception
        ConnPrintOutUtil.print(log, ConnPrintOutUtil.ERROR, e.getLocalizedMessage());
        ConnPrintOutUtil.printFailure(log);
      }
      count.incrementAndGet();
      ThreadUtils.doNotify(pool);
    }