簡體   English   中英

如何使用JDBC和Google-App-Script對多個查詢保持連接狀態?

[英]How can I keep my connection alive for multiple queries using JDBC and Google-App-Script?

我看到了這篇文章,但並沒有真正幫助我解決問題。

我使用的是在Google電子表格上打開的HTML對話框,其中包含一個插件,而我使用的是JBDC

我從多個查詢中加載了MYSQL數據庫中的一些數據,我還具有一個搜索欄來搜索數據庫中的數據,將來我希望HTML根據在HTML頁面中選擇的選項自動顯示各種數據庫值。 基本上,只有一個應用程序會有很多查詢,因此我猜應該有一個連接對象可以使用。

我已經嘗試了多種方法,可以用偽代碼向您展示。

  1. 每次打開一個新的連接

所以這將是我的GS文件

function firstFunc()
{
  var conn = Jdbc.getConnection(dbUrl, user, userPwd);
  //do my thing
  return (datas);
}
function secondFunc()
{
  var conn = Jdbc.getConnection(dbUrl, user, userPwd);
  //do my thing
  return (datas);
}
function thirdFunc()
{
  var conn = Jdbc.getConnection(dbUrl, user, userPwd);
  //do my thing
  return (datas);
}

然后是我的HTML

<script>
     var onSuccessFirst = function (data){
      //update my HTML with data
     }
     var onSuccessSecond = function (data){
      //update my HTML with data
     }
     var onSuccessThird = function (data){
      //update my HTML with data
     }
     google.script.run.withSuccessHandler(onSuccessFirst).firstFunc();
     google.script.run.withSuccessHandler(onSuccessSecond).secondFunc();
     google.script.run.withSuccessHandler(onSuccessThird).thirdFunc();
</script>

但是,當我使用免費的數據庫提供程序來開發第三個連接時,返回一條錯誤消息,提示我驗證密碼或用戶名,因為它無法連接到數據庫。

  1. 嘗試將連接從服務器傳遞到客戶端,然后再傳遞回服務器:

GS文件

function getConnection()
{
  return (Jdbc.getConnection(dbUrl, user, userPwd););
}
function firstFunc(conn)
{
  conn...
  //do my thing
  return (datas);
}
function secondFunc(conn)
{
  conn...
  //do my thing
  return (datas);
}
function thirdFunc(conn)
{
  conn...
  //do my thing
  return (datas);
}

然后是我的HTML

<script>
     var onSuccessFirst = function (data){
      //update my HTML with data
     }
     var onSuccessSecond = function (data){
      //update my HTML with data
     }
     var onSuccessThird = function (data){
      //update my HTML with data
     }
     var onSuccessConnection = function(conn)
     {
       google.script.run.withSuccessHandler(onSuccessFirst).firstFunc(conn);
       google.script.run.withSuccessHandler(onSuccessSecond).secondFunc(conn);
       google.script.run.withSuccessHandler(onSuccessThird).thirdFunc(conn);
     }
     google.script.run.withSuccessHandler(onSuccessConnection).getConnection();
</script>

但是這里connnull

輸入(搜索欄)為onchange時,我還會發送很多查詢,並且我使用的第一種方法有效,但是它不允許快速鍵入,因為它增加了每個鍵入字符的連接請求。

我能做什么?

也許嘗試使用第一種方法進行鏈接:

<script>
 var onSuccessThird = function (data){
  //update my HTML with data
 }
 var onSuccessSecond = function (data){
  //update my HTML with data
 google.script.run.withSuccessHandler(onSuccessThird).thirdFunc();
 }
 var onSuccessFirst = function (data){
  //update my HTML with data
 google.script.run.withSuccessHandler(onSuccessSecond).secondFunc();
 }
 google.script.run.withSuccessHandler(onSuccessFirst).firstFunc();
</script>

筆記:

  • google.script.run是一個異步函數。 在不等待上一個運行完成的情況下,將一次調用所有三個運行,這意味着在第一種方法中,幾乎同時打開了幾乎3個Jdbc連接。
  • 單個script.run調用將關閉連接。 因此,在第二種方法中,當第一次運行結束或之前, conn將為null。

    腳本完成執行后,JDBC連接將自動關閉。 (請注意,即使進行調用的HTML服務頁面保持打開狀態,單個google.script.run調用也算作完整執行。)

參考文獻:

如果您能夠使用連接池,則可以通過檢查null並為一組完整的請求響應僅打開/關閉一次連接來使一組完整的事務保持連接活動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM