如何批量阅读List

时间:2021-11-12 15:21:40

I have a function which reads a List of Notifications Object. Now I have to read the Notifications object in batches of 100(suppose) and update it.

我有一个函数,它读取通知列表对象。现在我必须以批量100(假设)读取Notifications对象并更新它。

public boolean addBSInfoToTemp(List<ParentNotification> pNotify)
  throws DaoException, BatchUpdateException {

int cnt = 0;

final String query = "insert into Temp values ?,?,?,'Y','N',?,?,?,?";

  while (!pNotify.isEmpty()) {
  try {
    pst = getConnection().prepareStatement(query);
    for (ParentNotification pn : pNotify) {
      cnt++;
      pst.setInt(1, pn.getUserId());
      pst.setString(2, pn.getEmail());
      pst.setString(3, pn.getNotificationType());

      Date createdDate = (Date) pn.getCreatedDate();
      pst.setDate(4, createdDate);

      pst.setString(5, pn.getCreatedBy());

      Date icsesCreatedDate = (Date) pn.getIcsesCreatedDate();
      pst.setDate(6, icsesCreatedDate);

      pst.setString(7, pn.getMiscellaneous());

      pst.addBatch();
      if(cnt==batchCount){
        break;
      }
    }

    int[] batch = pst.executeBatch();
    if (batch.length != 0) {
      flag = true;
    }
  } catch (BatchUpdateException b) {
    flag = false;
  } catch (SQLException sqlx) {
    flag = false;        
  } finally {
    close(pst, null);
  }
}
return flag;

}

What I am trying to do is read the List with batchCount = 100 then update and go back to 101 record and then update another 100 records untill the List is empty.

我想要做的是用batchCount = 100读取List然后更新并返回到101记录然后更新另外100条记录,直到List为空。

1 个解决方案

#1


0  

You have this:

你有这个:

while (!pNotify.isEmpty()) {

while(!pNotify.isEmpty()){

but I don't see that you ever remove objects from the list, so you have infinite loop. I would just make the outside loop to loop through all of the elements like this:

但我没有看到你从列表中删除对象,所以你有无限循环。我只是让外部循环遍历所有这些元素:

for (int i=0; i<=pNotify.size(); i++){

// and inside check if it is devisible by batch length 

      if(i % batchCount == 0){
        break;
      }

}

#1


0  

You have this:

你有这个:

while (!pNotify.isEmpty()) {

while(!pNotify.isEmpty()){

but I don't see that you ever remove objects from the list, so you have infinite loop. I would just make the outside loop to loop through all of the elements like this:

但我没有看到你从列表中删除对象,所以你有无限循环。我只是让外部循环遍历所有这些元素:

for (int i=0; i<=pNotify.size(); i++){

// and inside check if it is devisible by batch length 

      if(i % batchCount == 0){
        break;
      }

}