Statement cancelled due to timeout or client request

血红的双手。 提交于 2021-02-17 06:21:36

问题


I am trying to add data to my Google Cloud SQL database using Google App Script. My code was working fine last night when I finished but when I ran it this morning it is now giving me the error "statement cancelled due to timeout or client request", the error I am getting in the cloud console is "Got an error reading communication packets".

Last night it was processing about 1,600 lines of data. I have played around with it and narrowed it down that the error will occur when there is more the 15 lines. 15 lines it will add the data fine, change it to 16 lines and it throws the error, this tells me that it is not the connection itself. I have also tested the data in the spreadsheet by copying line 15 into line 16 and deleting all other data, this did not work. My main confusion with everything I have read saying it is timing out, is that it was working fine last night with 1,600 rows of data. My code is below:

function connection(folderId, db, c1, c2, c3, c4, c5, c6, c7) {
  var files = DriveApp.getFolderById(folderId).getFiles();
  var excelfile = files.next();
  var fileId = excelfile.getId();
  var data = SpreadsheetApp.openById(fileId).getSheetByName('Report 1');
  var last = data.getLastRow()
  var sheetdata=data.getRange("B5:H16").getValues();

  var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);//dburl is not defined

  conn.setAutoCommit(false); 
  var start = new Date();
  var stmt = conn.prepareStatement('INSERT INTO '+ db + ' ' + '('+c1+','+c2+','+c3+','+c4+','+c5+','+c6+','+c7+') values (?, ?, ?, ?, ?, ?, ?)');
  for (var i=0; i<sheetdata.length; i++) {
    stmt.setString(1, Utilities.formatDate(sheetdata[i][0], 'Etc/GMT', 'yyyy-MM-dd'));
    stmt.setString(2, sheetdata[i][1]);
    stmt.setString(3, sheetdata[i][2]);
    stmt.setString(4, sheetdata[i][3]);
    stmt.setString(5, sheetdata[i][4]);
    stmt.setString(6, sheetdata[i][5]);
    stmt.setString(7, sheetdata[i][6]);
    stmt.addBatch();
  }
      var batch = stmt.executeBatch();
      conn.commit();
      conn.close();

var end = new Date()
Logger.log('Time elapsed: %sms for %s rows.', end - start, batch.length);
}

回答1:


Found out that the issue is with the new App Script Runtime V8. For a fix, change your runtime back to Rhino. To do this go to "View > Show project manifest" then where it says "runtimeVersion": "V8" change this to be "runtimeVersion": "STABLE". There is currently an open bug on this issue here: https://issuetracker.google.com/issues/149413841




回答2:


Try it like this:

function connection(folderId, db, c1, c2, c3, c4, c5, c6, c7) {
  var ss=SpreadsheetApp.openById("fileId");//just go to the file and get the id
  var sh=ss.getSheetByName('Report 1');
  var rg=sh.getRange("B5:H16");
  var sheetdata=rg.getValues();
  var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);//dburl is not defined
  conn.setAutoCommit(false);
  var stmt = conn.prepareStatement('INSERT INTO '+ db + ' ' + '('+c1+','+c2+','+c3+','+c4+','+c5+','+c6+','+c7+') values (?, ?, ?, ?, ?, ?, ?)');
  for (var i=0; i<sheetdata.length; i++) {
    stmt.setString(1, Utilities.formatDate(sheetdata[i][0], Session.getScriptTimeZone(), 'yyyy-MM-dd'));
    stmt.setString(2, sheetdata[i][1]);
    stmt.setString(3, sheetdata[i][2]);
    stmt.setString(4, sheetdata[i][3]);
    stmt.setString(5, sheetdata[i][4]);
    stmt.setString(6, sheetdata[i][5]);
    stmt.setString(7, sheetdata[i][6]);
    stmt.addBatch();   
  }
  var batch = stmt.executeBatch();
  conn.commit();
  conn.close();
}


来源:https://stackoverflow.com/questions/62453690/statement-cancelled-due-to-timeout-or-client-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!