Advertisement
Guest User

Untitled

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