Advertisement
candyapplecorn

pool failure

Sep 7th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  SQL & Node.JS test program
  3.  *  Joseph Burger
  4.  *
  5.  *  Notes:
  6.  *  According to documentation @ "https://github.com/felixge/node-mysql#terminating-connections",
  7.  *  I should call pool.end(). However if I do, the rest of the program doesn't run, and the program
  8.  *  also hangs on the command line - unacceptable.
  9.  *
  10.  *  After hours, I arrived at this solution: rather than call connection.release() as the documentation
  11.  *  recommended, I call destroy (and not end() either). Additionally I don't call connection.release().
  12.  *
  13.  *  It's probably a bad answer to the problem but at least it works.
  14.  *
  15.  *  A bigger question is how the heck do I get results back from these asynchronous callbacks?
  16.  *  Sure, I can call functions from within callbacks. But that requires me to plan my programming
  17.  *  around that. Ugh.
  18.  *
  19.  */
  20. var mysql      = require('mysql');
  21. var pool = mysql.createPool({
  22.     host     : 'localhost',
  23.     user     : '*******',
  24.     password : '*******',
  25.     database : 'users',
  26. });
  27.  
  28. var fns = ["lina", "lanna", "cheryl", "heinrick", "bob", "larry", "william", "alice", "stewey", "jiang"],
  29.     lns = ["anderson", "bojangles", "williamson", "polerson", "dottson", "bjarnes", "gandertronf"];
  30.  
  31. var post = { username: fns[Math.floor(Math.random() * fns.length)],
  32.              password: lns[Math.floor(Math.random() * lns.length)] }
  33. function complex()
  34. {
  35.     pool.getConnection(function(err, connection)
  36.     {
  37.         console.log("Inserting: " + post.username + "\t" + post.password);
  38.         connection.query('insert into `user_info` SET ?', post,
  39.              function(err, result) {
  40.                   if (err) console.log (err);
  41.                   connection.destroy();
  42.                   getNum(); // +=+=+=+=+=
  43.                   console.log("\t\t-!-!- Exiting complex()");
  44.               });
  45.     });
  46. }
  47.  
  48. function getNum()
  49. {
  50.     pool.getConnection(function(err, connection){
  51.         connection.query('select * from `user_info`', function (error, rows, fields) {
  52.             if (error) throw error;
  53.  
  54.             ident = rows[Math.floor(Math.random() * rows.length)].ID;
  55.             console.log("Number of rows:\t" + rows.length);
  56.             console.log("ID to be deleted:\t" + ident);
  57.             connection.destroy();
  58.             del(ident); // +=+=+=+=+=
  59.             console.log("\t\t-!-!- Exiting getNum()");
  60.         });
  61.     });
  62. }
  63.  
  64. function del(ident)
  65. {
  66.     post = ['user_info', 'id', ident];
  67.  
  68.     pool.getConnection(function(err, connection)
  69.     {
  70.         var query = connection.query('DELETE FROM ?? WHERE ?? = ?', post, function(err, result)
  71.         {
  72.             if (err) throw err;
  73.             console.log('deleted ' + result.affectedRows + ' rows');
  74.             connection.destroy();
  75.             console.log("\t\t-!-!- Exiting del()");
  76.         });
  77.         console.log(query.sql);
  78.     });
  79. }
  80.  
  81. complex(); // +=+=+=+=+=
  82.  
  83. console.log("Heya bud! It's over!");
  84.  
  85. //pool.end();
  86.  
  87. // Output:
  88. /*
  89. Heya bud! It's over!
  90. Inserting: lina polerson
  91.         -!-!- Exiting complex()
  92. Number of rows: 72
  93. ID to be deleted:   2
  94.         -!-!- Exiting getNum()
  95. DELETE FROM `user_info` WHERE `id` = 2
  96. deleted 1 rows
  97.         -!-!- Exiting del()
  98. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement