[英]SQL query in Node.js server looping
I am trying to set up a server using Node.js. 我正在尝试使用Node.js设置服务器。 For the database I am using Xampp.
对于数据库,我正在使用Xampp。 I want to run the updateEngine() function when /update is provided.
我想在提供/ update时运行updateEngine()函数。 Everything is working , except the code is looping and after a while I get an error.
一切正常,除了代码正在循环,过一会儿我得到一个错误。 (I used
node --max_old_space_size=2000000
) (我使用了
node --max_old_space_size=2000000
)
Output 输出量
server is up!! connected as id 38 into the query connected as id 39 into the query
Error 错误
69777 ms: Mark-sweep 1398.8 (1456.7) -> 1398.8 (1456.7) MB, 1871.5 / 0 ms [allocation failure] [GC in old space requested]. 71705 ms: Mark-sweep 1398.8 (1456.7) -> 1398.8 (1456.7) MB, 1928.0 / 0 ms [allocation failure] [GC in old space requested]. 73346 ms: Mark-sweep 1398.8 (1456.7) -> 1398.8 (1456.7) MB, 1640.7 / 0 ms [last resort gc]. 75283 ms: Mark-sweep 1398.8 (1456.7) -> 1398.8 (1456.7) MB, 1937.4 / 0 ms [last resort gc].
==== JS stack trace ======== FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
==== JS堆栈跟踪========
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
Code 码
http = require('http'); var connect = require('connect'); var mysql = require('mysql'); var app = connect(); app.use('/update',updateEngine); http.createServer(app).listen(8080); console.log("server is up!!"); function updateEngine(request,response,done) { var connection = mysql.createConnection({ host: 'localhost', port: '3306', user: 'root', password: '', database: 'test' }); connection.connect(function(err) { if (err) { console.error('error connecting: ' + err.stack); return; } console.log('connected as id ' + connection.threadId); }); connection.query('SELECT * FROM `testtable`' , function (error, results, fields) { console.log("into the query"); }); connection.end(); }
Xampp Xampp
The MySQL server [mysqld] user = mysql port=3306 socket = /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock skip-external-locking key_buffer = 16M max_allowed_packet = 500M table_open_cache = 64 sort_buffer_size = 512K net_buffer_length = 8K read_buffer_size = 256K read_rnd_buffer_size = 512K myisam_sort_buffer_size = 8M
Suggestions: 意见建议:
Send a response to your requests (even if it's just a simple 200 OK with no body). 发送对您的请求的回复(即使只是一个简单的200 OK而没有正文)。 Right now it's never responding and so the client is probably timing out and automatically retrying the request.
目前它从未响应,因此客户端可能正在超时并自动重试请求。
Use a database connection pool that you create on startup instead of creating a new database connection for every single request. 使用在启动时创建的数据库连接池,而不是为每个单个请求创建一个新的数据库连接。 Using a connection pool is much more efficient since you are not creating and tearing down a connection every time (which adds overhead and latency to your requests).
使用连接池效率更高,因为您不必每次都创建和拆除连接(这会增加请求的开销和延迟)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.