以前,在Google Apps脚本中编写脚本时,我在所有功能中都打开和关闭了Jdbc连接。
为了加快我的应用程序的速度,似乎最好保持连接打开并在另一个功能中使用它。但是,我不知道如何在Google Apps脚本中执行此操作。
谁能给我建议或示例?
这是一个如何在ScriptDB上保留连接的示例。
function myFunction() {
var conn = ScriptDb.getMyDb().query({name : "my_connection"} );
var connection = null;
var itemId = null;
if(conn.getSize() == 0) {
Logger.log(" Connection not found, creating new one");
connection = Jdbc.getConnection('jdbc:mysql://<host>:3306/<instance>', 'user', 'password');
itemId = ScriptDb.getMyDb().save({name : "my_connection", connection : connection}).getId();
}else {
Logger.log(" Connection found, retrieving "+conn.getSize());
var dbItem = conn.next();
connection = dbItem.connection;
itemId = dbItem.getId();
}
//Check if it is closed
if(connection.isClosed()) {
connection = Jdbc.getConnection('jdbc:mysql://<host>:3306/<instance>', 'user', 'password');
//After opening remember to save to dabatase again and retain only one connection
ScriptDb.getMyDb().removeById(itemId);
ScriptDb.getMyDb().save({name : "my_connection", connection : connection});
}
//Use your connection here
//Remenber to close it based in some criteria
}
这里的例子。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句