您现在的位置:学赛首页 > IT认证 > sun认证 > 正文
JSP高访问量下的计数程序[2]
http://www.educity.cn 作者:佚名 来源:CSDN 2008年6月26日 发表评论 进入社区

  finally

  {

  try

  {

  if (ps != null)

  {

  ps.clearParameters();

  ps.close();

  ps = null;

  conn.close();

  }

  }

  catch (SQLException e) { }

  }

  }

  public long getLast()

  {

  return lastExecuteTime;

  }

  public void run()

  {

  long now = System.currentTimeMillis();

  if ((now - lastExecuteTime) > executeSep)

  {

  System.out.print("lastExecuteTime:"+lastExecuteTime);

  System.out.print(" now:"+now+"\n");

  System.out.print(" sep="+(now - lastExecuteTime)+"\n");

  lastExecuteTime=now;

  executeUpdate();

  }

  else

  {

  System.out.print("wait for "+(now - lastExecuteTime)+" seconds:"+"\n");

  }

  }

  }

  4.使用

  <% CountBean cb=new CountBean(); cb.setCountId(Integer.parseInt(request.getParameter("cid")));

  //计数的资源id CountCache.add(cb);

  System.out.print(CountCache.list.size()+"
");

  CountControl c=new CountControl();

  c.run();

  System.out.print(CountCache.list.size()+"
");

  %>

  private static long executeSep = 30000;// 定义更新间隔时间,单位毫秒 半分钟

  public CountControl()

  {

  }

  public synchronized void executeUpdate()

  {

  Connection conn = null;

  PreparedStatement ps = null;

  try

  {

  conn = new DBConnection().getConn();

  conn.setAutoCommit(false);

  ps = conn

  .prepareStatement("update t_news set hits=hits+1 where id=?");

  for (int i = 0; i < CountCache.list.size(); i++)

  {

  CountBean cb = (CountBean) CountCache.list.getFirst();

  CountCache.list.removeFirst();

  ps.setInt(1, cb.getCountId());

  ps.executeUpdate();

  // ps.addBatch();

  }

  // int[] counts = ps.executeBatch();

  conn.commit();

  }

  catch (Exception e)

  {

  e.printStackTrace();

  }

[1]  [2]