mirror of
https://github.com/thinkjs/thinkjs.git
synced 2026-01-25 14:42:47 +00:00
修复mysql操作数据量过大导致连接丢失的情况
This commit is contained in:
parent
d5feaafa43
commit
dc330c770b
@ -8,13 +8,11 @@ var mysql = require("mysql");
|
||||
module.exports = Class(function(){
|
||||
"use strict";
|
||||
return {
|
||||
handle: null,
|
||||
config: null,
|
||||
deferred: null,
|
||||
init: function(config){
|
||||
this.handle = null;
|
||||
this.config = config;
|
||||
this.deferred = null;
|
||||
this.tryTimes = 0;
|
||||
},
|
||||
/**
|
||||
* 建立数据库连接
|
||||
@ -22,7 +20,7 @@ module.exports = Class(function(){
|
||||
*/
|
||||
connect: function(){
|
||||
if (this.handle) {
|
||||
return true;
|
||||
return this.deferred.promise;
|
||||
}
|
||||
var self = this;
|
||||
var deferred = getDefer();
|
||||
@ -57,11 +55,12 @@ module.exports = Class(function(){
|
||||
})
|
||||
//连接句柄
|
||||
this.handle = connection;
|
||||
//把上一次的promise resolve
|
||||
//把上一次的promise reject
|
||||
if (this.deferred) {
|
||||
this.deferred.resolve();
|
||||
this.deferred.reject(new Error("connection closed"));
|
||||
}
|
||||
this.deferred = deferred;
|
||||
return this.deferred.promise;
|
||||
},
|
||||
/**
|
||||
* 查询sql语句,返回一个promise
|
||||
@ -73,13 +72,19 @@ module.exports = Class(function(){
|
||||
console.log("sql: " + sql);
|
||||
}
|
||||
var self = this;
|
||||
self.connect();
|
||||
return this.deferred.promise.then(function(){
|
||||
return this.connect().then(function(){
|
||||
var deferred = getDefer();
|
||||
self.handle.query(sql, function(err, rows){
|
||||
if (err) {
|
||||
//当数据量非常大时,可能会出现连接丢失,这里进行重连
|
||||
if (err.code === 'PROTOCOL_CONNECTION_LOST' && self.tryTimes < 3) {
|
||||
self.tryTimes++;
|
||||
self.close();
|
||||
return self.query(sql);
|
||||
}
|
||||
return deferred.reject(err);
|
||||
}
|
||||
self.tryTimes = 0;
|
||||
return deferred.resolve(rows || []);
|
||||
});
|
||||
return deferred.promise;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user