Sometimes you have to count the number of clicks for each article, and if you want to update the library every time you browse, that performance in the traffic is very large, the pressure on the server will be very large, a better way is to update the data first cached, and then use the database for a period of batch processing, batch update library. The source code is as follows:
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;
/**
*
* @author http://www.tot.name
*/
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 October 18, 2006, 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.*;
/**
*
* @author Http://www.tot.name
*/
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 October 18, 2006, 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.*;
/**
*
* @author Http://www.tot.name
*/
public class countcontrol{
Private static long lastexecutetime=0;//last update time
Private static long executesep=60000;//defines the update interval, in milliseconds
/** 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");
}
}
}
Note: If your database driver supports batch processing, you can remove the comments in front of the ⑵,⑶ tagged code and annotate the code before ⑴
Class is written, the following is called in the JSP below.
<%
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>");
%>