pool_connection.js 1.5 KB

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