由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 求助: node.js memory leak
相关主题
菜鸟问个node.js问题怎么把 integer 转为 multi-byte integer format?
node.js, express, sails, angularjs在一起做project,折磨人啊大家来看看这个纯Javascript实现的QR二维码生成器
请教一下:Javascript callback not working求教, python 对于很奇怪的字符的encoding 怎么处理?
node.js错误求指点用react的试过中文么?
同步编程真郁闷encode high cardinality categorical features
Node.js 并发模型相关:closure 中 access mutable variable一道C++面试编程题
尼玛 callback 真是反人类有人能解释一下这段C++代码吗
linux, find command question帮帮看看这段tree insertion
相关话题的讨论汇总
话题: err话题: function话题: test话题: callback话题: sql
进入Programming版参与讨论
1 (共1页)
q******n
发帖数: 66
1
如果我comment out line 88, 就没问题。 各为大牛, handle_database有什么问题吗?
{code}
1 var express = require('express');
2 var app = express();
3 var mysql = require('mysql');
4 var pool = mysql.createPool({
5 connectionLimit : 100, //important
6 host : 'localhost',
7 user : 'root',
8 password : '',
9 database : 'callback',
10 debug : false
11 });
12
13 function handle_database(req,res) {
14 pool.getConnection(function(err,connection){
15 if (err) {
16 connection.release();
17 return;
18 }
19 var sql = "";
20
21 connection.query("select COUNT(*) from notifications",
function(err,rows){
22 if(!err) {
23 if(req.body.eventType == ‘TEST_START'){
24 sql = {test_id: ''' + req.body.testID + ''',
25 event_type: ''' + req.body.eventType + ''',
26 report_id: ''' + req.body.reportID + ''',
27 test_start: + req.body.testStart,
28 agent: ''' + req.body.agent + '''};
29 connection.query('INSERT INTO notifications SET
?', sql, function(err, result) {
30 if (err) throw err;
31 });
32 }
33 else if(req.body.eventType == ‘TEST_ISSUE'){
34 sql = {test_id: ''' + req.body.testID + ''',
35 event_type: ''' + req.body.eventType + ''',
36 report_id: ''' + req.body.reportID + ''',
37 timestamp: + req.body.timestamp,
38 severity: ''' + req.body.severity + ''',
39 message: ''' + req.body.message + '''};
40 connection.query('INSERT INTO notifications SET
?', sql, function(err, result) {
41 if (err) throw err;
42 });
43 }
44 else if(req.body.eventType == ‘TEST_COMPLETE'){
45 sql = {test_id: ''' + req.body.testID + ''',
46 event_type: ''' + req.body.eventType + ''',
47 report_id: ''' + req.body.reportID + ''',
48 test_end: + req.body.testEnd,
49 duration: + req.body.duration ,
50 test_status: ''' + req.body.testStatus + ''
'};
51 connection.query('INSERT INTO notifications SET
?', sql, function(err, result) {
52 if (err) throw err;
53 });
54 }
55 else if(req.body.eventType == 'REPORT_AVAILABLE'){
56 sql = {test_id: ''' + req.body.testID + ''',
57 event_type: ''' + req.body.eventType + ''',
58 report_id: ''' + req.body.reportID + ''',
59 timestamp: + req.body.timestamp ,
60 failure_message: ''' + req.body.
failureMessage + '''};
61 connection.query('INSERT INTO notifications SET
?', sql, function(err, result) {
62 if (err) throw err;
63 });
64 }
65 else {
66 console.log("ERROR: unknown messagern");
67 console.log(req.body);
68 }
69 sql = null;
70 }
71 });
72
73 connection.on('error', function(err) {
74 console.log("ERROR: unknown DB errorrn");
75 return;
76 }).setMaxListeners(0);
77 connection.release();
78 });
79 }
80
81 var port = process.env.PORT || 8080;
82 var bodyParser = require('body-parser');
83 app.use(bodyParser.json()); // support json encoded bodies
84 app.use(bodyParser.urlencoded({ extended: true })); // support
encoded bodies
85
86 // POST http://localhost:8080/callback/forfun
87 app.post('/callback/forfun’, function(req, res) {
88 handle_database(req,res);
89 res.sendStatus(200);
90 res.end();
91 }).setMaxListeners(0);
92
93 // start the server
94 app.listen(port);
95 console.log('Server started! At http://localhost:' + port);
{code}
s*i
发帖数: 5025
2
有可能是connection.query 还没有返回结果,提前connection.release()了。注意
javascript asynchronous的。我感觉你应该把connection.release()放到query的
callback body里。
c*********e
发帖数: 16335
3
86 // POST http://localhost:8080/callback/forfun
87 app.post('/callback/forfun’, function(req, res) {
88 handle_database(req,res);
89 res.sendStatus(200);
90 res.end();
91 }).setMaxListeners(0);
说明你没弄懂node.js的异步是啥意思,还有callback.

吗?

【在 q******n 的大作中提到】
: 如果我comment out line 88, 就没问题。 各为大牛, handle_database有什么问题吗?
: {code}
: 1 var express = require('express');
: 2 var app = express();
: 3 var mysql = require('mysql');
: 4 var pool = mysql.createPool({
: 5 connectionLimit : 100, //important
: 6 host : 'localhost',
: 7 user : 'root',
: 8 password : '',

d****n
发帖数: 1637
4
87 app.post('/callback/forfun’, function(req, res) {
88 handle_database(req,res);
89 res.sendStatus(200);
90 res.end();
91 }).setMaxListeners(0);
==>
app.post('/callback/forfun', function(req, res){
async.series([
function handleDatabase(callback){
//db operations,
mysqlQuery = "select * from table";
query.exec(function (e ,r ){
if (e) return callback(e);
return callback(null, r);
})

},
function saveTodatabase(callback){
r.key = 1234; //mock update
r.save(callback); //if r can save
},
function closeDb(callback){
db_handler.close(callback);
}
],
function done(e, r){
if (e) return res.badRequest(e);

return res.ok(r);
});
})
r*****i
发帖数: 234
5
最后一个对res操作的人负责close
1 (共1页)
进入Programming版参与讨论
相关主题
帮帮看看这段tree insertion同步编程真郁闷
[合集] 如何得到一个指向STL元素的指针?Node.js 并发模型相关:closure 中 access mutable variable
cgi测试newbee问题尼玛 callback 真是反人类
一个类的非静态成员函数可以用作回调函吗?linux, find command question
菜鸟问个node.js问题怎么把 integer 转为 multi-byte integer format?
node.js, express, sails, angularjs在一起做project,折磨人啊大家来看看这个纯Javascript实现的QR二维码生成器
请教一下:Javascript callback not working求教, python 对于很奇怪的字符的encoding 怎么处理?
node.js错误求指点用react的试过中文么?
相关话题的讨论汇总
话题: err话题: function话题: test话题: callback话题: sql