批量入库 BatchPersistent 02

public static class StopHookThread extends Thread
{
public void run()
{
persistentAlldata();
}
}

private static void persistentAlldata()
{
TableName_Field[] tnfs = null;
while (true)
{
tnfs = getTNFFromlList();
if (tnfs == null || tnfs.length == 0)
{
break;
}
else
{
batchInsertToDB(tnfs);
}
}
}

private static TableName_Field[] getTNFFromlList()
{
TableName_Field[] subList = null;
synchronized (object)
{
// 如果数组中的数据量超过每批次入库的量,只取批次入库的量,
// 例如每批只能有100条数据,
// 但是总的数据量有10000条,实际可以取出只能100条

int maxeaxhBatch = Tools.getMaxeachBatch();
if (count >= maxeaxhBatch)
{
TableName_Field[] allsql = new TableName_Field[sqlList.length];
subList = new TableName_Field[maxeaxhBatch];
// 每次取出的数量
System.arraycopy(sqlList, 0, subList, 0, maxeaxhBatch);
// 还没有取出的数量
System.arraycopy(sqlList, 0, allsql, 0, count - maxeaxhBatch);
sqlList = allsql;
count = count - maxeaxhBatch;
}
else if (count > 0)// 取出所有的数据
{
subList = new TableName_Field[count];
System.arraycopy(sqlList, 0, subList, 0, count);
sqlList = new TableName_Field[INITSIZE];
count = 0;
}
}
return subList;
}

private static void batchInsertToDB(TableName_Field[] tnfs)
{
try
{
logger.debug("Batch SQL insert into db is ready size = "
+ tnfs.length);
OperateDB.batchInsert(tnfs);
logger.debug("Batch SQL insert into db is successfully! size = "
+ tnfs.length);
}
catch (Exception e)
{
// 写入数据库失败,需要将这些数据写入日志中
StringBuffer sb = null;
for (TableName_Field tnf : tnfs)
{
sb = new StringBuffer(500);
sb.append("insert DB is fail, tablename = ").append(
tnf.getTableName());
for (EsbTableField etf : tnf.getList())
{
sb.append("\n").append("field name:")
.append(etf.getFieldName()).append(" Value:")
.append(etf.getFieldValue());
}
logger.error(sb.toString());
}
logger.error("batchInsertToDB has error msg:" + e.getMessage());
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值