简体   繁体   中英

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

I saw this post but it didn't really helped me solving my problem.

I use a HTML dialog box opened with an add-on on google spreadsheets and I'm using JBDC .

I load from multiple queries some datas from my MYSQL database, I also have a search bar to search for datas in the DB and in the future I would like my HTML to automatically show various database values depending on option choosen in my HTML page. Basically that's a lot of queries for only one App so I guess there should be one connection object to use.

I've tried multiple things that I can show you in pseudo-code.

  1. open a new connection every time

so here would be my GS file

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);
}

and then my 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>

But as I'm using a free database provider for developping the third connection returns me an error telling me to verify password or username because it failed to connect to database.

  1. try passing the connection from the server to the client then back to the server:

GS file

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);
}

and then my 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>

But here conn is null .

I also have a lot of queries sent when my input (search bar) is onchange and I use first method it works except it doesn't allow quick typing as it increases connection requests on each character typed.

What can I do?

Maybe Try chaining with your first approach:

<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>

Notes:

  • google.script.run is a async function. All three runs will be called one by one without waiting for the the previous run to finish, which means almost 3 Jdbc connections will be opened more or less at the same time in the first approach.
  • A single script.run call will close the connection. So in your second approach, the conn will be null when the first run is over or before that.

    JDBC connections close automatically when a script finishes executing. (Keep in mind that a single google.script.run call counts as a complete execution, even if the HTML service page that made the call remains open.)

References:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM