pool_connection.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. const Connection = require('../index.js').Connection;
  3. class PoolConnection extends Connection {
  4. constructor(pool, options) {
  5. super(options);
  6. this._pool = pool;
  7. // When a fatal error occurs the connection's protocol ends, which will cause
  8. // the connection to end as well, thus we only need to watch for the end event
  9. // and we will be notified of disconnects.
  10. // REVIEW: Moved to `once`
  11. this.once('end', () => {
  12. this._removeFromPool();
  13. });
  14. this.once('error', () => {
  15. this._removeFromPool();
  16. });
  17. }
  18. release() {
  19. if (!this._pool || this._pool._closed) {
  20. return;
  21. }
  22. this._pool.releaseConnection(this);
  23. }
  24. promise(promiseImpl) {
  25. const PromisePoolConnection = require('../promise').PromisePoolConnection;
  26. return new PromisePoolConnection(this, promiseImpl);
  27. }
  28. end() {
  29. const err = new Error(
  30. 'Calling conn.end() to release a pooled connection is ' +
  31. 'deprecated. In next version calling conn.end() will be ' +
  32. 'restored to default conn.end() behavior. Use ' +
  33. 'conn.release() instead.'
  34. );
  35. this.emit('warn', err);
  36. // eslint-disable-next-line no-console
  37. console.warn(err.message);
  38. this.release();
  39. }
  40. destroy() {
  41. this._removeFromPool();
  42. super.destroy();
  43. }
  44. _removeFromPool() {
  45. if (!this._pool || this._pool._closed) {
  46. return;
  47. }
  48. const pool = this._pool;
  49. this._pool = null;
  50. pool._removeConnection(this);
  51. }
  52. }
  53. PoolConnection.statementKey = Connection.statementKey;
  54. module.exports = PoolConnection;
  55. // TODO: Remove this when we are removing PoolConnection#end
  56. PoolConnection.prototype._realEnd = Connection.prototype.end;