JSP high access quantity of the counting program

Source: Internet
Author: User
Tags comments

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 October 18, 2006, 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 batching, then you can remove the comments in front of the ⑵,⑶ markup code and add comments before the code ⑴


The
class is written, and 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 () + "
");
Countcontrol c=new Countcontrol ();
C.run ();
Out.print (CountCache.list.size () + "
");
%>



 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.