批量入库 BatchPersistent 03

/**
* SQL语句存入队列中,等待处理线程来获取。 如果数据超过队列的最大数据量,并且写线程挂死, 那么就直接写数据库。
* 2012-1-10,Xgw123485
*/
public static void putSQLTosqlList(TableName_Field[] tnfs)
{
logger.debug("Put TableName_Field to queue and original quence size is :"
+ count);
// 原则上数据不会出现超过最大数据量的,但是万一处理线程真的挂死,
// 这段代码判断只是堆杖的保护,防止内存泄露
if (sqlList.length > Tools.getMaxLength() || (!thread.isAlive()))
{
logger.error("Please check the BatchThread becuase the "
+ "queue is full or Batch Thread is dead");
// 如果写数据库线程还活着,直接将数据写入数据库,线程处理不及
if (thread.isAlive())
{
batchInsertToDB(tnfs);
}
else
{
// 线程挂死
logger.error("Please check the BatchThread because that is dead!");
batchInsertToDB(tnfs);
if (count != 0)
{
persistentAlldata();
}

}
return;
}
// 将存入sql语句存入等待批量写入数据库的SQL数组中
synchronized (object)
{
// 如果数组大小已经超过初始化的数组的大小500,需要重新创建一个更大的数组
if (sqlList.length < (count + tnfs.length))
{
TableName_Field[] temp = new TableName_Field[INITSIZE
+ tnfs.length + count];
System.arraycopy(sqlList, 0, temp, 0, count);
System.arraycopy(tnfs, 0, temp, count, tnfs.length);
sqlList = temp;
count += tnfs.length;
}
else
// 直接将SQL存入数组中
{
System.arraycopy(tnfs, 0, sqlList, count, tnfs.length);
count += tnfs.length;
}
logger.debug("new queue size count : " + count);

}
// 如果数据量超过每批次最大处理量,需要唤醒处理线程起来处理
if (count > Tools.getMaxeachBatch())
{
synchronized (waitobject)
{
waitobject.notifyAll();
logger.debug("wake up! More data waitting for you to do handle, the size of sqlList:"
+ count);
}
}
}

}
经验分享 程序员 微信小程序 职场和发展