国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

實例代碼:JSP高訪問量下的計數(shù)程序

2024-09-05 00:19:49
字體:
供稿:網(wǎng)友

  有時要為每一篇文章統(tǒng)計其點擊次數(shù),如果每一次瀏覽都要更新一次庫的話,那性能在訪問量很大的情況下,服務(wù)器的壓力就會很大了,比較好一點的方法就是先將要更新的數(shù)據(jù)緩存起來,然后每隔一段時間再利用數(shù)據(jù)庫的批量處理,批量更新庫。源碼如下:

  countbean.java

/*
* countdata.java
*
* created on 2006年10月18日, 下午4:44
*
* to change this template, choose tools | options and locate the template under
* the source creation and management node. right-click the template and choose
* open. you can then make changes to the template in the source editor.
*/

  package com.tot.count;

/**
*
*/
public class countbean {
 private string counttype;
 int countid;
 /** creates a new instance of countdata */
 public countbean() {}
 public void setcounttype(string counttypes){
  this.counttype=counttypes;
 }
 public void setcountid(int countids){
  this.countid=countids;
 }
 public string getcounttype(){
  return counttype;
 }
 public int getcountid(){
  return countid;
 }
}

  countcache.java

/*
* countcache.java
*
* created on 2006年10月18日, 下午5:01
*
* to change this template, choose tools | options and locate the template under
* the source creation and management node. right-click the template and choose
* open. you can then make changes to the template in the source editor.
*/
package com.tot.count;
import java.util.*;
/**
*
*/
public class countcache {
 public static linkedlist list=new linkedlist();
 /** creates a new instance of countcache */
 public countcache() {}
 public static void add(countbean cb){
  if(cb!=null){
   list.add(cb);
  }
 }
}

 countcontrol.java

 /*
 * countthread.java
 *
 * created on 2006年10月18日, 下午4:57
 *
 * to change this template, choose tools | options and locate the template under
 * the source creation and management node. right-click the template and choose
 * open. you can then make changes to the template in the source editor.
 */
package com.tot.count;
import tot.db.dbutils;
import java.sql.*;
/**
*/
public class countcontrol{
 private static long lastexecutetime=0;//上次更新時間 
 private static long executesep=60000;//定義更新間隔時間,單位毫秒
 /** creates a new instance of countthread */
 public countcontrol() {}
 public synchronized void executeupdate(){
  connection conn=null;
  preparedstatement ps=null;
  try{
   conn = dbutils.getconnection();
   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();
  } finally{
  try{
   if(ps!=null) {
    ps.clearparameters();
ps.close();
ps=null;
  }
 }catch(sqlexception e){}
 dbutils.closeconnection(conn);
 }
}
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");
 }
}
}

//注:如果你的數(shù)據(jù)庫驅(qū)動支持批處理,那么可以將⑵,⑶標記的代碼前的注釋去掉,同時在代碼⑴前加上注釋

  類寫好了,下面是在jsp中如下調(diào)用。

<%
countbean cb=new countbean();
cb.setcountid(integer.parseint(request.getparameter("cid")));
countcache.add(cb);
out.print(countcache.list.size()+"<br>");
countcontrol c=new countcontrol();
c.run();
out.print(countcache.list.size()+"<br>");
%>

,歡迎訪問網(wǎng)頁設(shè)計愛好者web開發(fā)。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 伊通| 环江| 香河县| 青阳县| 井陉县| 大宁县| 五原县| 德惠市| 巴东县| 阿克陶县| 交城县| 阳泉市| 牙克石市| 湘阴县| 乌兰浩特市| 长泰县| 蓝山县| 抚顺市| 丰城市| 射阳县| 临泽县| 盱眙县| 邓州市| 广元市| 宁河县| 涟水县| 乌海市| 杂多县| 伊吾县| 宝丰县| 临汾市| 沾益县| 来凤县| 英吉沙县| 尉氏县| 信宜市| 德安县| 华池县| 乐山市| 镇平县| 隆子县|