Skip to content

Fix for MySQL Pooling Async Bug #1710

Closed

Description

@Antzy

The NodeJS MySQL server.js code has an async Pool creation call createPool() without any await. So when it is immediately followed by ensureSchema() which runs a query on the pool, it throws an error as the pool isn't yet created:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'query' of undefined

This is the same example on Google Cloud Docs and I assumed them to be correct and wasted 2 days on this thinking something wrong at my end. Please fix this so even any other call can ensure that pool object is properly created before call.

Link to code:
https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/cloud-sql/mysql/mysql/server.js

Activity

  1. Antzy commented on Apr 9, 2020

    @Antzy
    Author

    Might not be the optimum way but thus is how I solved it:

    var createPoolPromise = createPool();
    // [END cloud_sql_mysql_mysql_create]
    
    const ensureSchema = async () => {
      await createPoolPromise;
      // Wait for tables to be created (if they don't already exist).
      await pool.query(
        `CREATE TABLE IF NOT EXISTS votes
          ( vote_id SERIAL NOT NULL, time_cast timestamp NOT NULL,
          candidate CHAR(6) NOT NULL, PRIMARY KEY (vote_id) );`
      );
    };
    ensureSchema();
    

    I saved the returned Promise from createPool() and added await for it before every query. Not the prettiest solution but is there any better way?

  2. fhinkel commented on Apr 9, 2020

    @fhinkel
    Contributor

    @Antzy 👋 Thanks for raising the issue. We'll fix it!

  3. added
    type: docsImprovement to the documentation for an API.
    api: cloudsqlIssues related to the Cloud SQL for MySQL API.
    on Apr 9, 2020
  4. shubha-rajan commented on Apr 9, 2020

    @shubha-rajan
    Contributor

    Thanks for raising the issue! I think using a middleware function to ensure pool creation might be a cleaner approach. The problem I see is that even if we await the promise to create the pool before each query, if table creation hasn't completed before those other queries, the other queries will fail. I think we should actually be awaiting the promise from ensureSchema before other queries. I opened #1712 with a possible fix

  5. Antzy commented on Apr 12, 2020

    @Antzy
    Author

    Thanks for looking into this. My schema is pretty much guaranteed to be already in place, so I was await-ing pool creation before every query. But you are right, much better to await ensureSchema instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api: cloudsqlIssues related to the Cloud SQL for MySQL API.type: docsImprovement to the documentation for an API.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions