PoolConnection.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var inherits = require('util').inherits;
  2. var Connection = require('./Connection');
  3. var Events = require('events');
  4. module.exports = PoolConnection;
  5. inherits(PoolConnection, Connection);
  6. function PoolConnection(pool, options) {
  7. Connection.call(this, options);
  8. this._pool = pool;
  9. // Bind connection to pool domain
  10. if (Events.usingDomains) {
  11. this.domain = pool.domain;
  12. }
  13. // When a fatal error occurs the connection's protocol ends, which will cause
  14. // the connection to end as well, thus we only need to watch for the end event
  15. // and we will be notified of disconnects.
  16. this.on('end', this._removeFromPool);
  17. this.on('error', function (err) {
  18. if (err.fatal) {
  19. this._removeFromPool();
  20. }
  21. });
  22. }
  23. PoolConnection.prototype.release = function release() {
  24. var pool = this._pool;
  25. if (!pool || pool._closed) {
  26. return undefined;
  27. }
  28. return pool.releaseConnection(this);
  29. };
  30. // TODO: Remove this when we are removing PoolConnection#end
  31. PoolConnection.prototype._realEnd = Connection.prototype.end;
  32. PoolConnection.prototype.end = function () {
  33. console.warn(
  34. 'Calling conn.end() to release a pooled connection is ' +
  35. 'deprecated. In next version calling conn.end() will be ' +
  36. 'restored to default conn.end() behavior. Use ' +
  37. 'conn.release() instead.'
  38. );
  39. this.release();
  40. };
  41. PoolConnection.prototype.destroy = function () {
  42. Connection.prototype.destroy.apply(this, arguments);
  43. this._removeFromPool(this);
  44. };
  45. PoolConnection.prototype._removeFromPool = function _removeFromPool() {
  46. if (!this._pool || this._pool._closed) {
  47. return;
  48. }
  49. var pool = this._pool;
  50. this._pool = null;
  51. pool._purgeConnection(this);
  52. };