From 280e6411bbb59305bf2b611b006366feffc30cea Mon Sep 17 00:00:00 2001 From: welefen Date: Thu, 12 Dec 2013 14:03:41 +0800 Subject: [PATCH] update --- lib/Lib/Driver/Db/MysqlDb.class.js | 8 +--- lib/Lib/Driver/Socket/MysqlSocket.class.js | 51 +++++++++++++++++----- package.json | 2 +- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/lib/Lib/Driver/Db/MysqlDb.class.js b/lib/Lib/Driver/Db/MysqlDb.class.js index 07c0f4b8..6fe741b8 100644 --- a/lib/Lib/Driver/Db/MysqlDb.class.js +++ b/lib/Lib/Driver/Db/MysqlDb.class.js @@ -37,12 +37,9 @@ var db = module.exports = Db(function(){ query: function(str){ this.initConnect(false); if (!this._linkID) { - return false; + return get_promise(false); }; this.queryStr = str; - if (this.queryID) { - this.free(); - }; N('db_query', 1); var self = this; return this._linkID.query(str).then(function(ret){ @@ -64,9 +61,6 @@ var db = module.exports = Db(function(){ return false; }; this.queryStr = str; - if (this.queryID) { - this.free(); - }; N('db_execute', 1); var self = this; return this._linkID.query(str).then(function(ret){ diff --git a/lib/Lib/Driver/Socket/MysqlSocket.class.js b/lib/Lib/Driver/Socket/MysqlSocket.class.js index 05d2a7d0..9d568cce 100644 --- a/lib/Lib/Driver/Socket/MysqlSocket.class.js +++ b/lib/Lib/Driver/Socket/MysqlSocket.class.js @@ -8,15 +8,33 @@ var mysql = require("mysql"); var socket = module.exports = Class(function(){ return { connect: null, + error : "", init: function(config){ this.connect = null; + this.error = ""; + var self = this; + //创建连接 var connection = mysql.createConnection({ host : config.host || "localhost", user : config.user || "root", password : config.password || "", database : config.database || "" }); - connection.connect(); + //连接 + connection.connect(function(err){ + if (err) { + console.log('error when connecting to db:', err); + }; + }); + //错误时重新连接 + connection.on("error", function(err){ + if(err.code === 'PROTOCOL_CONNECTION_LOST') { + self.init(config); + } else { + self.error = err; + console.log("db error:" , err); + } + }) this.connect = connection; }, /** @@ -29,20 +47,30 @@ var socket = module.exports = Class(function(){ console.log(sql); }; var deferred = when.defer(); - this.connect.query(sql, function(err, rows, fields){ - if (err) { - return deferred.resolve({ + var self = this; + if (this.error) { + process.nextTick(function(){ + deferred.resolve({ errno: 1, - errmsg: err, + errmsg: "db error", + }); + }) + }else{ + this.connect.query(sql, function(err, rows, fields){ + if (err) { + return deferred.resolve({ + errno: 1, + errmsg: err, + fields: fields + }); + }; + deferred.resolve({ + errno: 0, + data: rows || [], fields: fields }); - }; - deferred.resolve({ - errno: 0, - data: rows || [], - fields: fields }); - }) + } return deferred.promise; }, /** @@ -51,6 +79,7 @@ var socket = module.exports = Class(function(){ */ close: function(){ this.connect && this.connect.end(); + this.connect = null; } } }) \ No newline at end of file diff --git a/package.json b/package.json index ff323f9a..e96dfc72 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "thinkjs", "description": "thinkphp web framework for nodejs", - "version": "0.1.19", + "version": "0.1.21", "author": { "name": "welefen", "email": "welefen@gmail.com"