title: NodeJS Child process cannot access MySQL Pool object
body: <p>My intention is upon a file upload the main Node.JS Express backend process will fork a child process which will call a java program to parse the uploaded file and then the NodeJS child will make bulk (100K+) MySQL queries. I want the child to handle the queries because I do not want the parent process to &quot;block.&quot; Since both the parent and child processes need access to the MySQL connection I have defined in an external file containing an <code>npm mysql</code> pool.</p>
  <p><code>mysqlConnector.js:</code></p>
  <pre><code>// Load module
  var mysql = require('mysql');
  // Initialize pool
  var pool      =    mysql.createPool({
      connectionLimit : 10,
      host     : 'localhost',
      port: 3306,
      user: 'bob',
      password: 'my_pass',
      database: 'my_db'
  });    
  
  const doQuery = (query_string) =&gt; {
    console.log(pool);
    pool.query(query_string, function (error, results, fields) {
      if (error) throw error;
      console.log('in doQuery: query' );
    });
  };
  
  exports.doQuery = doQuery;
  exports.pool = pool
  </code></pre>
  <p>My index.js (main process) is able to access this pool and make queries with no issue. The main process will fork the child running the following code</p>
  <p><code>handleprops.js:</code></p>
  <pre><code>const {execSync, fork} = require('child_process');
  const {doQuery} = require('../mysqlConnector');
  
  const exec_options = {
      cwd: null,
      env: null,
      encoding: 'utf8',
      timeout: 0,
      maxBuffer: 200 * 1024,
      killSignal: 'SIGTERM'
  };
  
  
  process.on('message', msg=&gt;{
  
    if(msg.filepath &amp;&amp; msg.username){
      execSync(`java -jar C:\\Users\\colli\\d2reader\\out\\artifacts\\d2reader_jar\\d2reader.jar ${msg.filepath}`, exec_options);
  
      var filename = msg.filepath.replace(/^.*[\\\/]/, '');
      filename = __dirname + &quot;\\json\\&quot; + filename.substring(0, filename.length-3) + &quot;json&quot;;
      try{
        const fs = require('fs');
        const data = fs.readFileSync(filename, 'utf8');
        processSharedStashQueries(JSON.parse(data), msg.username);
      } catch(err) {
        console.error(err);
      }
      process.exit();
    }
  });
  
  const processSharedStashQueries = (data, username) =&gt; {
      const INSERT_USER_QUERY = `insert into sharedstashes  (NumStashes, SharedGold, NumItems, UserID) values (${data.numStashes}, ${data.sharedGold}, ${data.numItems}, (select id from users where username = '${username}'))`;
      doQuery(INSERT_USER_QUERY);
  
  };
  </code></pre>
  <p>When the child calls <code>doQuery()</code> inside <code>processSharedStashQueries()</code> the pool object will be logged and I can verify it is a valid and defined object and is the same exact pool object the parent process uses. However, nothing happens when the child hits the <code>pool.query()</code> line of code. No error and no console logs. It's as if nothing happens.</p>
  <p>Why can the child process &quot;see&quot; the pool object but not be able to use it?</p>
  <p>Is my overall approach incorrect? I fear I might be missing some knowledge in how to use npm mysql effectively to do such a thing. Should I put the MySQL query smarts inside the external java program? Or is that bad design?</p>
  <p>Thanks in advance!</p>