pool-promise.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. const Connection = require('./connection');
  3. const PoolBase = require('./pool-base');
  4. const Errors = require('./misc/errors');
  5. const util = require('util');
  6. function PoolPromise(options) {
  7. const processTaskPromise = function (conn, sql, values, isBatch) {
  8. if (sql) {
  9. const fct = isBatch ? conn.batch : conn.query;
  10. return fct(sql, values)
  11. .then((res) => {
  12. conn.releaseWithoutError();
  13. return Promise.resolve(res);
  14. })
  15. .catch((err) => {
  16. conn.releaseWithoutError();
  17. return Promise.reject(err);
  18. });
  19. }
  20. return Promise.resolve(conn);
  21. };
  22. /**
  23. * Add connection to pool.
  24. */
  25. const createConnectionPoolPromise = function (pool) {
  26. const conn = new Connection(options.connOptions);
  27. return conn
  28. .connect()
  29. .then(() => {
  30. if (pool.closed) {
  31. conn
  32. .end()
  33. .then(() => {})
  34. .catch(() => {});
  35. return Promise.reject(
  36. Errors.createError(
  37. 'Cannot create new connection to pool, pool closed',
  38. true,
  39. null,
  40. '08S01',
  41. Errors.ER_ADD_CONNECTION_CLOSED_POOL,
  42. null
  43. )
  44. );
  45. }
  46. conn.releaseWithoutError = () => {
  47. conn.release().catch(() => {});
  48. };
  49. conn.forceEnd = conn.end;
  50. conn.release = () => {
  51. if (pool.closed) {
  52. pool._discardConnection(conn);
  53. return Promise.resolve();
  54. }
  55. if (options.noControlAfterUse) {
  56. pool._releaseConnection(conn);
  57. return Promise.resolve();
  58. }
  59. //if server permit it, reset the connection, or rollback only if not
  60. // COM_RESET_CONNECTION exist since mysql 5.7.3 and mariadb 10.2.4
  61. // but not possible to use it with mysql waiting for https://bugs.mysql.com/bug.php?id=97633 correction.
  62. // and mariadb only since https://jira.mariadb.org/browse/MDEV-18281
  63. let revertFunction = conn.rollback;
  64. if (
  65. options.resetAfterUse &&
  66. conn.info.isMariaDB() &&
  67. ((conn.info.serverVersion.minor === 2 && conn.info.hasMinVersion(10, 2, 22)) ||
  68. conn.info.hasMinVersion(10, 3, 13))
  69. ) {
  70. revertFunction = conn.reset;
  71. }
  72. return revertFunction()
  73. .then(() => {
  74. pool._releaseConnection(conn);
  75. return Promise.resolve();
  76. })
  77. .catch((err) => {
  78. //uncertain connection state.
  79. // discard it
  80. pool._discardConnection(conn);
  81. return Promise.resolve();
  82. });
  83. };
  84. conn.end = conn.release;
  85. return Promise.resolve(conn);
  86. })
  87. .catch((err) => {
  88. return Promise.reject(err);
  89. });
  90. };
  91. PoolBase.call(this, options, processTaskPromise, createConnectionPoolPromise, (conn) =>
  92. conn.ping(options.pingTimeout)
  93. );
  94. }
  95. util.inherits(PoolPromise, PoolBase);
  96. module.exports = PoolPromise;