NodeJS Server与mysql挂起 [英] NodeJS Server with mysql hangs
问题描述
我正在使用NodeJS,Express,mysql编写一个应用程序,目前为止,一切正常,但是当我的应用程序经过一段时间,当mysql连接中断时,我的应用程序通过这个异常,我的应用程序关闭。
错误:读取ECONNRESET
在errnoException(net.js:901:11)
在TCP.onread(net.js :556:19)
从另一个stackquestion我知道我必须处理这样未被捕获的异常,如这个。
process.on('uncaughtException',function(err){
console.log :'+ err);
console.log(err.stack);
});
此后,我的应用程序不会退出,而是挂断,所以我的问题是如何我处理这个异常,以便即使在这个异常之后,我的应用程序没有挂断,mysql连接也是可以的。
我发现解决方案,如果别人面临同样的问题,我可以帮助你们。
首先我捕获了所有未捕获的异常,这使我的应用程序不会退出第二,挂起的问题是因为当服务器关闭连接时,我所要求的所有查询将失败,我的服务器将简单地挂断,我猜其节点-mysql错误,但是我使用连接池解决了它,在池中如果服务器关闭连接后再次接收到一秒钟以上,所以我的问题解决了这种方式。
这是怎么回事最大限度地利用node-mysql pooling
I am writing an application using NodeJS, Express, mysql, so far everything works fine, but when I run my application after sometime when mysql connection is interrupted my application throughs this exception and my application goes down.
Error: read ECONNRESET
at errnoException (net.js:901:11)
at TCP.onread (net.js:556:19)
From another stackquestion i came to know that i have to handle such uncaught exceptions like this.
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
console.log(err.stack);
});
after this now my application does not exit, but instead it hangs up, so my question is how do I handle this exception so that mysql connection is ok even after this exception and my application does not hang up.
I found the solution and I am posting if someone else is facing the same problem this might help you guys as well.
First I caught all uncaught exceptions, which made my application not to exit abnormally.
Second the problem of hanging I had was because when server would close the connection, all my requested queries would fail and my server would simply hang up I guess its node-mysql bug, but I solved it using connecting pooling, in pooling if server close the connection its re-aquired after a second or so again so my problem was solved this way.
Here is how to make most out of node-mysql pooling
这篇关于NodeJS Server与mysql挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!