1 | var inherits = require('util').inherits;
|
2 | var Connection = require('./Connection');
|
3 | var Events = require('events');
|
4 |
|
5 | module.exports = PoolConnection;
|
6 | inherits(PoolConnection, Connection);
|
7 |
|
8 | function PoolConnection(pool, options) {
|
9 | Connection.call(this, options);
|
10 | this._pool = pool;
|
11 |
|
12 |
|
13 | if (Events.usingDomains) {
|
14 | this.domain = pool.domain;
|
15 | }
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | this.on('end', this._removeFromPool);
|
21 | this.on('error', function (err) {
|
22 | if (err.fatal) {
|
23 | this._removeFromPool();
|
24 | }
|
25 | });
|
26 | }
|
27 |
|
28 | PoolConnection.prototype.release = function release() {
|
29 | var pool = this._pool;
|
30 |
|
31 | if (!pool || pool._closed) {
|
32 | return undefined;
|
33 | }
|
34 |
|
35 | return pool.releaseConnection(this);
|
36 | };
|
37 |
|
38 |
|
39 | PoolConnection.prototype._realEnd = Connection.prototype.end;
|
40 |
|
41 | PoolConnection.prototype.end = function () {
|
42 | console.warn(
|
43 | 'Calling conn.end() to release a pooled connection is ' +
|
44 | 'deprecated. In next version calling conn.end() will be ' +
|
45 | 'restored to default conn.end() behavior. Use ' +
|
46 | 'conn.release() instead.'
|
47 | );
|
48 | this.release();
|
49 | };
|
50 |
|
51 | PoolConnection.prototype.destroy = function () {
|
52 | Connection.prototype.destroy.apply(this, arguments);
|
53 | this._removeFromPool(this);
|
54 | };
|
55 |
|
56 | PoolConnection.prototype._removeFromPool = function _removeFromPool() {
|
57 | if (!this._pool || this._pool._closed) {
|
58 | return;
|
59 | }
|
60 |
|
61 | var pool = this._pool;
|
62 | this._pool = null;
|
63 |
|
64 | pool._purgeConnection(this);
|
65 | };
|