Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 0d4f37bd
authored
Oct 20, 2014
by
José Moreira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dialect.mysql.connection-manager: handleDisconnects
1 parent
16523b00
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
91 additions
and
2 deletions
lib/dialects/abstract/connection-manager.js
lib/dialects/mysql/connection-manager.js
lib/promise.js
test/mysql/connector-manager.test.js
lib/dialects/abstract/connection-manager.js
View file @
0d4f37b
...
@@ -7,7 +7,7 @@ var Pooling = require('generic-pool')
...
@@ -7,7 +7,7 @@ var Pooling = require('generic-pool')
max
:
5
,
max
:
5
,
min
:
0
,
min
:
0
,
idle
:
10000
,
idle
:
10000
,
handleDisconnects
:
fals
e
handleDisconnects
:
tru
e
}
}
,
ConnectionManager
;
,
ConnectionManager
;
...
...
lib/dialects/mysql/connection-manager.js
View file @
0d4f37b
...
@@ -58,6 +58,24 @@ ConnectionManager.prototype.connect = function(config) {
...
@@ -58,6 +58,24 @@ ConnectionManager.prototype.connect = function(config) {
return
;
return
;
}
}
if
(
config
.
pool
.
handleDisconnects
)
{
// Connection to the MySQL server is usually
// lost due to either server restart, or a
// connnection idle timeout (the wait_timeout
// server variable configures this)
//
// See [stackoverflow answer](http://stackoverflow.com/questions/20210522/nodejs-mysql-error-connection-lost-the-server-closed-the-connection)
connection
.
on
(
'error'
,
function
(
err
)
{
if
(
err
.
code
===
'PROTOCOL_CONNECTION_LOST'
)
{
// Get the right read/write pool in case of replication, otherwise use generic pool
var
pool
=
config
.
replication
?
self
.
pool
[
connection
.
queryType
]
:
self
.
pool
;
// Remove it from read/write pool
pool
.
destroy
(
connection
);
}
});
}
resolve
(
connection
);
resolve
(
connection
);
});
});
...
@@ -66,6 +84,13 @@ ConnectionManager.prototype.connect = function(config) {
...
@@ -66,6 +84,13 @@ ConnectionManager.prototype.connect = function(config) {
});
});
};
};
ConnectionManager
.
prototype
.
disconnect
=
function
(
connection
)
{
ConnectionManager
.
prototype
.
disconnect
=
function
(
connection
)
{
// Dont disconnect connections with an ended protocol
// That wil trigger a connection error
if
(
connection
.
_protocol
.
_ended
)
{
return
Promise
.
resolve
();
}
return
new
Promise
(
function
(
resolve
,
reject
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
connection
.
end
(
function
(
err
)
{
connection
.
end
(
function
(
err
)
{
if
(
err
)
return
reject
(
err
);
if
(
err
)
return
reject
(
err
);
...
@@ -74,7 +99,7 @@ ConnectionManager.prototype.disconnect = function(connection) {
...
@@ -74,7 +99,7 @@ ConnectionManager.prototype.disconnect = function(connection) {
});
});
};
};
ConnectionManager
.
prototype
.
validate
=
function
(
connection
)
{
ConnectionManager
.
prototype
.
validate
=
function
(
connection
)
{
return
connection
&&
[
'disconnected'
,
'protocol_error'
].
indexOf
(
connection
.
state
)
!
==
-
1
;
return
connection
&&
[
'disconnected'
,
'protocol_error'
].
indexOf
(
connection
.
state
)
=
==
-
1
;
};
};
module
.
exports
=
ConnectionManager
;
module
.
exports
=
ConnectionManager
;
lib/promise.js
View file @
0d4f37b
test/mysql/connector-manager.test.js
View file @
0d4f37b
...
@@ -52,5 +52,69 @@ if (Support.dialectIsMySQL()) {
...
@@ -52,5 +52,69 @@ if (Support.dialectIsMySQL()) {
})
})
})
})
})
})
// This should run only on direct mysql
if
(
Support
.
dialectIsMySQL
(
true
))
{
it
(
'should maintain connection'
,
function
()
{
var
sequelize
=
Support
.
createSequelizeInstance
({
pool
:
{
min
:
1
,
max
:
1
,
handleDisconnects
:
true
,
idle
:
5000
}}),
cm
=
sequelize
.
connectionManager
,
conn
;
return
sequelize
.
sync
()
.
then
(
function
()
{
return
cm
.
getConnection
();
})
.
then
(
function
(
connection
)
{
// Save current connection
conn
=
connection
;
})
.
then
(
function
()
{
return
cm
.
releaseConnection
(
conn
);
})
.
then
(
function
()
{
// Get next available connection
return
cm
.
getConnection
();
})
.
then
(
function
(
connection
)
{
// Old threadId should be different from current new one
expect
(
conn
.
threadId
).
to
.
be
.
equal
(
connection
.
threadId
);
expect
(
cm
.
validate
(
conn
)).
to
.
be
.
ok
;
return
cm
.
releaseConnection
(
connection
);
});
});
it
(
'should work with handleDisconnects'
,
function
()
{
var
sequelize
=
Support
.
createSequelizeInstance
({
pool
:
{
min
:
1
,
max
:
1
,
handleDisconnects
:
true
,
idle
:
5000
}}),
cm
=
sequelize
.
connectionManager
,
conn
;
return
sequelize
.
sync
()
.
then
(
function
(){
return
cm
.
getConnection
();
})
})
.
then
(
function
(
connection
)
{
// Save current connection
conn
=
connection
;
// simulate a unexpected end
connection
.
_protocol
.
end
();
})
.
then
(
function
()
{
return
cm
.
releaseConnection
(
conn
);
})
.
then
(
function
()
{
// Get next available connection
return
cm
.
getConnection
();
})
.
then
(
function
(
connection
)
{
// Old threadId should be different from current new one
expect
(
conn
.
threadId
).
to
.
not
.
be
.
equal
(
connection
.
threadId
);
expect
(
cm
.
validate
(
conn
)).
to
.
not
.
be
.
ok
;
return
cm
.
releaseConnection
(
connection
);
});
});
}
});
}
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment