PoolConnection.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var inherits = require('util').inherits;
  2. var Connection = require('./Connection')
  3. var __changeUser = Connection.prototype.changeUser;
  4. module.exports = PoolConnection;
  5. inherits(PoolConnection, Connection);
  6. function PoolConnection(pool, options) {
  7. Connection.call(this, options);
  8. this._pool = pool;
  9. this._purge = false
  10. // When a fatal error occurs the connection's protocol ends, which will cause
  11. // the connection to end as well, thus we only need to watch for the end event
  12. // and we will be notified of disconnects.
  13. this.on('end', this._removeFromPool);
  14. this.on('error', this._removeFromPool);
  15. }
  16. PoolConnection.prototype.changeUser = function changeUser(options, callback) {
  17. this._purge = true;
  18. return __changeUser.apply(this, arguments);
  19. };
  20. PoolConnection.prototype.release = function release() {
  21. var pool = this._pool;
  22. if (!pool || pool._closed) {
  23. return;
  24. }
  25. return pool.releaseConnection(this);
  26. };
  27. // TODO: Remove this when we are removing PoolConnection#end
  28. PoolConnection.prototype._realEnd = Connection.prototype.end;
  29. PoolConnection.prototype.end = function () {
  30. console.warn( '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.release();
  36. };
  37. PoolConnection.prototype.destroy = function () {
  38. this._removeFromPool(this);
  39. return Connection.prototype.destroy.apply(this, arguments);
  40. };
  41. PoolConnection.prototype._removeFromPool = function(connection) {
  42. if (!this._pool || this._pool._closed) {
  43. return;
  44. }
  45. var pool = this._pool;
  46. this._pool = null;
  47. pool._removeConnection(this);
  48. };