代码之家  ›  专栏  ›  技术社区  ›  JSmith

如何使用JDBC和googleapp脚本保持多个查询的连接?

  •  0
  • JSmith  · 技术社区  · 7 年前

    我看见了 this post

    HTML 打开一个对话框,在google电子表格上有一个插件,我正在使用 JBDC

    我从多个查询中加载一些数据 MYSQL 数据库,我也有一个搜索栏来搜索数据库中的数据,在未来,我想我的HTML自动显示各种数据库值取决于我的选择 HTML格式

    我已经尝试了很多可以用伪代码展示的东西。

    所以这是我的 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. 尝试将连接从服务器传递到客户端,然后再传递回服务器:

    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>
    

    但是在这里 conn null .

    我也有很多查询发送时,我的输入(搜索栏)是 onchange 我使用第一种方法,它可以工作,除了它不允许快速键入,因为它增加了每个字符的连接请求。

    我能做什么?

    2 回复  |  直到 7 年前
        1
  •  2
  •   TheMaster    7 年前

    <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连接会自动关闭(请记住,一个google.script.run调用将算作一个完整的执行,即使发出该调用的HTML服务页保持打开状态。)

        2
  •  1
  •   urs_ng    7 年前

    如果您能够使用连接池, 否则,您可以通过检查null并对一组完整的请求-响应仅打开/关闭一次连接,使连接在一组完整的事务中保持活动状态。