Herbert
Herbert

Reputation: 580

JDBC on Google Apps Script. Exception: Statement cancelled due to timeout or client request

I am trying to fetch data from mySQL database on Google Cloud SQL using JDBC from Google Apps Script. However, I got this error:

Exception: Statement cancelled due to timeout or client request

I can fetch some other data successfully. However, some data I can't.

I execute one of the successful queries and one of the unsuccessful queries on mySQL workbench. I can execute the unsuccessful query with no problem on mySQL workbench.

I compared the durations.

Duration / Fetch
-------------------------------------------
Successful query:     0.140 sec / 0.016 sec
Unsuccessful query:   0.406 sec / 0.047 sec

The unsuccessful query seems to take longer. So, I set query timeout with:

stmt.setQueryTimeout(0);

intending to set no timeout (when the value is set to zero it means that the execution has no timeout limit). Then, I executed it on Google Apps Script.

However, it doesn't work and get the same error. Could you tell me a solution for this?

Upvotes: 6

Views: 6335

Answers (5)

haochao2016
haochao2016

Reputation: 53

I got this issue when I submit the update statement for the same records in mysql table. I set the breakpoint before update statement in my program, and I start the 2 process to run this program. so, the first process will update the mysql table correctly and the second process will get this exception later.

you need add the 'for update ' in you select statement. so the second process will got the zero not the exception when you update the record in the transaction.

Upvotes: 0

maxhugen
maxhugen

Reputation: 1944

@TheMaster: Trying out connection to MySQL, same time out issue, even when I tried the example at https://developers.google.com/apps-script/guides/jdbc > Write 500 rows of data to a table in a single batch.

Even worse, when I reverted to the rhino interface, the result was the same. That crashes my whole development approach! :(

[Edit] FWIW, it seems to me that both Rhino and V8 don't like keeping a connection (or is it the statement?) open long enough for the above prepareStatement to complete.

So I tried inserting the 500 records as per above linked example, using a prepared SQL statement, which worked OK:

  var conn = sqlGetConnection();
  var start = new Date();

  // conn.setAutoCommit(false);
  // var stmt = conn.prepareStatement('INSERT INTO entries '
  //     + '(guestName, content) values (?, ?)');
  // for (var i = 0; i < 500; i++) {
  //   stmt.setString(1, 'Name ' + i);
  //   stmt.setString(2, 'Hello, world ' + i);
  //   stmt.addBatch();
  // }
  // var batch = stmt.executeBatch();
  // conn.commit();

  var sql = 'INSERT INTO ' +
    'entries (guestName, content) ' +
    'values ';
  for (var i = 0; i < 500; i++) {
    var col1 = "'" + 'Name ' + i + "'";           // Note that the strings had to be ecapsulated
    var col2 = "'" + 'Hello, world ' + i + "'";   // in quotes to work with this method
    sql = sql + '(' + col1 + ', ' + col2 + '),';
  }
  sql = sql.substr(0,sql.length-1);
  var stmt = conn.createStatement();
  var response = stmt.executeUpdate(sql);  // executeQuery is only for SELECT statements

  conn.close();

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

Upvotes: 0

Rob
Rob

Reputation: 575

Update to add 2nd fix

After some trial and error I figured out what solved this for me -- Some queries worked, others returned this error.

Fix 1 The common denominator was that it was the queries that had been converted to the multi-line format by the V8 engine / new editor that had this issue.

As an example, switching to the new editor / V8 converted long text strings to be similar to the following:

var query = "select Street_Number, street_name, street_suffix, street_dir_prefix, postal_code, city, mls, address, unitnumber " 
  +"as unit, uspsid,latitude,longitude from properties.forsale where status = 'active' and zpid is null and property_type = 'residential'"

This query resulted in the error as described. The fix is changing longer queries to be continuous strings like the following:

var query = "select Street_Number, street_name, street_suffix, street_dir_prefix, postal_code, city, mls, address, unitnumber as unit, uspsid,latitude,longitude from properties.forsale where status = 'active' and zpid is null and property_type = 'residential'"

Fix 2

This one was a bit more frustrating. V8 is not as forgiving when it comes to connections to the database. Previous to V8 It would automatically close any connections that lingered, however, it looks like V8 does not like that. I had originally written my scripts to share as few connections as possible but I noticed there were some that I got this error on and it was the ones where a connection might be 'split.' For example:

var conn = getconnection() //this is a connection function I have written
  var date = date || Utilities.formatDate(new Date(), "America/Chicago", "yyyy-MM-dd");

  var keyobj = getkeys(undefined,undefined,conn);
  conn = keyobj.conn;
  var results = sqltojson(query, true, conn);

The function above was causing this error, and I'm assuming it's because the 'conn' variable was being returned from the function but, I'm going to make a wild assumption, that the connection for whatever reason cannot be in two separate functions at once since it was being both returned and continued to be a value in the object 'keyobj' and also as 'conn'. Adding delete keyobj.conn immediately after defining the conn variable did the trick:

var conn = getconnection() //this is a connection function I have written
  var date = date || Utilities.formatDate(new Date(), "America/Chicago", "yyyy-MM-dd");

  var keyobj = getkeys(undefined,undefined,conn);
  conn = keyobj.conn;
  delete keyobj.conn;
  var results = sqltojson(query, true, conn);

Doing both of these fixes stopped this error and allowed the script to continue without problems.

Upvotes: 1

Khai Cin
Khai Cin

Reputation: 1

The issue was the same as mentioned above and it failed on today so I tried to change the old version and it works for me. FYI

Upvotes: 0

TheMaster
TheMaster

Reputation: 50890

This seems to be a known issue. Star ★ and comment on the issue to get Google developers to prioritise the issue. Until the issue is fixed, you can switch back to rhino runtime.

Upvotes: 8

Related Questions