Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. let mysqlConnection = getFreeConnection();
  2.  
  3. function getFreeConnection() {
  4. pool = getConnectionsPool();
  5. try {
  6. pool.getConnection(function cb(err, connection) {
  7.  
  8. if (err) {
  9. console.error(`error getting connection from pool; ${err}`);
  10. throw err;
  11. }
  12.  
  13. if (!connection) {
  14. console.error(`error getting connection from pool`);
  15. }
  16. return connection;
  17. });
  18. } catch (e) {
  19. console.error(e);
  20. }
  21. }
  22.  
  23. function getConnectionsPool() {
  24. pool = pool ?
  25. pool :
  26. mysql.createPool({
  27. host: process.env.MYSQL_HOST,
  28. user: process.env.MYSQL_USER,
  29. password: process.env.MYSQL_PASSWORD,
  30. database: process.env.MYSQL_DB
  31. });
  32.  
  33. if (!pool) {
  34. console.error(`error getting connections pool`);
  35. return null;
  36. }
  37. return pool;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement