上面我说过每条请求进来我们都会将用户信息插入到数据库,400个同时进来就同时有400条sql插入,一个插入占一个连接,这样很危险的,经常导致连接撑爆,所以我们在这个地方直接起了一个线程,把大量的需要插入的数据放入线程的List里面,然后在线程里面做批量插入操作,这样的情况就是数据量大的时候只用了一个Connection就可以将300 400条数据插入到数据库,节省了大量的连接资源啊!我这个项目除了插入log数据量大之外,还有个Push功能,push广告之类的时候,每秒有1000以上的请求进来,在插入log表的时候还会插入push表,你说这样多伤啊,所以push这块也做了这样的优化操作.
思路就是上面那样,优化的结果就是,想都不用想,妈妈再也不用担心应用连接数据库会超时了,除非网速坑爹.
希望以上思路和优化解决方案可以帮助到各位朋友,我们马上也会使用到hadoop,到时候再来和大家分享经验.下面我先给出部分实例代码
1.插入log的线程代码
packagecom.xxx.appstore.util;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.util.ArrayList;
importjava.util.List;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importcom.xxx.appstore.Constants;
importcom.xxx.appstore.UserData;
importcom.xxx.common.util.db.DBUtil;
publicclass InsertLogThread extendsThread{
privatestatic Logger logger = LoggerFactory.getLogger(InsertLogThread.class);
privatestatic List<UserData> saveList =new ArrayList<UserData>();
privatestatic List<UserData> actionList =new ArrayList<UserData>();
public InsertLogThread(){
}
publicvoid addUserData(UserData data){
synchronized(saveList){
saveList.add(data);
saveList.notify();
}
}
publicvoid run(){
while(true){
while(saveList.size()> 0){
actionList.add(saveList.remove(0));
if(actionList.size()> 2000){
break;
}
}
insertLog(actionList);
actionList.clear();
try{
synchronized(saveList){
saveList.wait();
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
/**
* 插入本次请求。
*
*/
publicvoid insertLog(List<UserData> actionList){
Connection con =null;
PreparedStatement pst =null;
String sql ="insert into tblLog(TId,UuId,Imsi,Brand,Model,Channel,Plat,AndroidVer,ScreenSize,Lang,AppStoreVer,Provider,ConnectionMode,GetLocType,LocStr,country,province,city,IpAddr,AccessType,CurrPage,ProPage,proContent,AppId,OtherParas,Created,"+
"phone,product,sdk,display,codename,tCardSize,RAM,cpuClockSpeed,source,smsCenter,enc,pVer,imei,pkg) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,sysdate,"+
"?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
try{
con = DBUtil.getConnection(Constants.dbName);
long id = DBUtil.getNextSeq(con, "seq_tbllog_id");
pst = con.prepareStatement(sql);
for(int j =0; j < actionList.size(); j++){
UserData data = actionList.get(j);
pst.setLong(1, id);
pst.setString(2, data.aid);
pst.setString(3, data.imsi);
pst.setString(4, data.brand);
pst.setString(5, data.model);
pst.setString(6, data.key);
pst.setString(7, data.har);
pst.setString(8, data.release);
pst.setString(9, data.sc);
pst.setString(10, data.lang);
pst.setString(11, data.storeVer);
pst.setString(12, data.providerName);
pst.setString(13, data.netType);
pst.setString(14, null);
pst.setString(15, data.loc);
pst.setString(16, data.country);
pst.setString(17, data.province);
pst.setString(18, data.city);
pst.setString(19, data.ip);
pst.setString(20, getString(data.currentRequestType, 50));
pst.setString(21, getString(data.currentRequestContent, 50));
pst.setString(22, getString(data.lastRequestType, 50));
pst.setString(23, getString(data.lastRequestContent, 50));
pst.setString(24, data.appId);
pst.setString(25, getString(data.otherParams, 150));
pst.setString(26, data.phoneNum);
pst.setString(27, data.product);
pst.setString(28, data.sdk);
pst.setString(29, data.dis);
pst.setString(30, data.code);
pst.setString(31, data.tcard);
pst.setString(32, data.ram);
pst.setString(33, data.fre);
pst.setString(34, data.source);
pst.setString(35, data.smsCenter);
pst.setString(36, data.enc);
pst.setInt(37, data.pVer);
pst.setString(38, data.imei);
pst.setString(39, data.pkg);
pst.addBatch();
}
pst.executeBatch();
}catch(Exception e){
logger.error("插入日志失败", e);
}finally{
DBUtil.closePreparedStatement(pst);
DBUtil.closeConnection(con);
}
}
publicString getString(String str, int length){
if(str ==null){
return str;
}
while(str.getBytes().length> length){
str = str.substring(0, str.length()- 4);
}
return str;
}
}
2.调用时候的代码,下面3句代码分别在不同的代码块,考虑到公司保密问题我就不把代码都拿出来了,只列出和本方案有关系的代码
publicstatic InsertLogThread thread =new InsertLogThread();//再某个相关类调用的最上面实例话这个线程
thread.start();//在某个初始化的地方启动线程
thread.addUserData(userData);//在需要做线程操作的地方写入此代码 userData可以是对象也可以是值,看你们需要
干货分享完毕,准备下班了.
作者:Darren中
原文地址: http://www.darrenzhong.com/?p=1504
原文:http://blog.csdn.net/darrenzhong/article/details/44812337