Skip to content

Commit a188a4d

Browse files
committed
add PoolConnection which inherits from Connection and implements all the pool-spesific Connection behavior
1 parent e9f8ad6 commit a188a4d

File tree

7 files changed

+70
-70
lines changed

7 files changed

+70
-70
lines changed

lib/Connection.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var Net = require('net');
22
var ConnectionConfig = require('./ConnectionConfig');
3-
var Pool = require('./Pool');
43
var Protocol = require('./protocol/Protocol');
54
var SqlString = require('./protocol/SqlString');
65
var Query = require('./protocol/sequences/Query');

lib/Pool.js

+4-63
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var mysql = require('../');
22
var Connection = require('./Connection');
33
var EventEmitter = require('events').EventEmitter;
44
var Util = require('util');
5+
var PoolConnection = require('./PoolConnection');
56

67
module.exports = Pool;
78

@@ -35,7 +36,7 @@ Pool.prototype.getConnection = function (cb) {
3536
}
3637

3738
if (this.config.connectionLimit === 0 || this._allConnections.length < this.config.connectionLimit) {
38-
connection = this._createConnection();
39+
connection = new PoolConnection(this, { config: this.config.connectionConfig });
3940

4041
this._allConnections.push(connection);
4142

@@ -68,7 +69,7 @@ Pool.prototype.getConnection = function (cb) {
6869
Pool.prototype.releaseConnection = function (connection) {
6970
var cb;
7071

71-
if (connection._poolRemoved) {
72+
if (!connection._pool) {
7273
// The connection has been removed from the pool and is no longer good.
7374
if (this._connectionQueue.length) {
7475
cb = this._connectionQueue.shift();
@@ -115,11 +116,7 @@ Pool.prototype.end = function (cb) {
115116

116117
for (var i = 0; i < this._allConnections.length; i++) {
117118
connection = this._allConnections[i];
118-
119-
delete connection.release;
120-
connection.destroy = connection._realDestroy;
121-
connection.end = connection._realEnd;
122-
connection.end(endCB);
119+
connection._realEnd(endCB);
123120
}
124121
};
125122

@@ -139,61 +136,9 @@ Pool.prototype.query = function (sql, values, cb) {
139136
});
140137
};
141138

142-
Pool.prototype._createConnection = function() {
143-
var self = this;
144-
var connection = (this.config.createConnection)
145-
? this.config.createConnection(this.config.connectionConfig)
146-
: mysql.createConnection(this.config.connectionConfig);
147-
148-
connection._realEnd = connection.end;
149-
connection.end = function(cb) {
150-
console.warn( 'Calling conn.end() to release a pooled connection is '
151-
+ 'deprecated. In next version calling conn.end() will be '
152-
+ 'restored to default conn.end() behavior. Use '
153-
+ 'conn.release() instead.'
154-
);
155-
this.release();
156-
if (cb) cb();
157-
};
158-
159-
connection.release = function () {
160-
self.releaseConnection(connection);
161-
};
162-
163-
connection._realDestroy = connection.destroy;
164-
connection.destroy = function() {
165-
self._removeConnection(connection);
166-
connection.destroy();
167-
};
168-
169-
// When a fatal error occurs the connection's protocol ends, which will cause
170-
// the connection to end as well, thus we only need to watch for the end event
171-
// and we will be notified of disconnects.
172-
connection.on('end', this._handleConnectionEnd.bind(this, connection));
173-
connection.on('error', this._handleConnectionError.bind(this, connection));
174-
175-
return connection;
176-
};
177-
178-
Pool.prototype._handleConnectionEnd = function(connection) {
179-
if (this._closed || connection._poolRemoved) {
180-
return;
181-
}
182-
this._removeConnection(connection);
183-
};
184-
185-
Pool.prototype._handleConnectionError = function(connection) {
186-
if (this._closed || connection._poolRemoved) {
187-
return;
188-
}
189-
this._removeConnection(connection);
190-
};
191-
192139
Pool.prototype._removeConnection = function(connection) {
193140
var i;
194141

195-
connection._poolRemoved = true;
196-
197142
for (i = 0; i < this._allConnections.length; i++) {
198143
if (this._allConnections[i] === connection) {
199144
this._allConnections.splice(i, 1);
@@ -208,9 +153,5 @@ Pool.prototype._removeConnection = function(connection) {
208153
}
209154
}
210155

211-
delete connection.release;
212-
connection.end = connection._realEnd;
213-
connection.destroy = connection._realDestroy;
214-
215156
this.releaseConnection(connection);
216157
};

lib/PoolConfig.js

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var ConnectionConfig = require('./ConnectionConfig');
44
module.exports = PoolConfig;
55
function PoolConfig(options) {
66
this.connectionConfig = new ConnectionConfig(options);
7-
this.createConnection = options.createConnection || undefined;
87
this.waitForConnections = (options.waitForConnections === undefined)
98
? true
109
: Boolean(options.waitForConnections);

lib/PoolConnection.js

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

test/common.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ common.createConnection = function(config) {
3131

3232
common.createPool = function(config) {
3333
config = mergeTestConfig(config);
34-
config.createConnection = common.createConnection;
34+
config.connectionConfig = mergeTestConfig(config.connectionConfig);
3535
return Mysql.createPool(config);
3636
};
3737

test/integration/pool/test-destroy-connection.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ pool.getConnection(function(err, connection) {
99
connection.destroy();
1010

1111
assert.ok(pool._allConnections.length == 0);
12-
assert.ok(connection._poolRemoved);
13-
assert.strictEqual(connection.end, Connection.prototype.end);
14-
assert.strictEqual(connection.end, Connection.prototype.end);
15-
assert.ok(!('release' in connection))
12+
assert.ok(!connection._pool);
1613

1714
pool.end();
1815
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var common = require('../../common');
2+
var assert = require('assert');
3+
var Connection = require(common.lib + '/Connection');
4+
var PoolConnection = require(common.lib + '/PoolConnection');
5+
var EventEmitter = require('events').EventEmitter;
6+
var pool = common.createPool();
7+
8+
pool.getConnection(function (err, conn) {
9+
if (err) throw err;
10+
11+
assert(conn instanceof PoolConnection);
12+
assert(conn instanceof Connection);
13+
assert(conn instanceof EventEmitter);
14+
15+
pool.end();
16+
});

0 commit comments

Comments
 (0)