|
楼主 |
发表于 2022-4-23 05:24:57
|
显示全部楼层
- react-server | node:events:504
- react-server | throw er; // Unhandled 'error' event
- react-server | ^
- react-server |
- react-server | Error: Connection lost: The server closed the connection.
- react-server | at Protocol.end (/app/server/node_modules/mysql/lib/protocol/Protocol.js:112:13)
- react-server | at Socket.<anonymous> (/app/server/node_modules/mysql/lib/Connection.js:94:28)
- react-server | at Socket.<anonymous> (/app/server/node_modules/mysql/lib/Connection.js:526:10)
- react-server | at Socket.emit (node:events:538:35)
- react-server | at endReadableNT (node:internal/streams/readable:1345:12)
- react-server | at processTicksAndRejections (node:internal/process/task_queues:83:21)
- react-server | Emitted 'error' event on Connection instance at:
- react-server | at Connection._handleProtocolError (/app/server/node_modules/mysql/lib/Connection.js:423:8)
- react-server | at Protocol.emit (node:events:526:28)
- react-server | at Protocol._delegateError (/app/server/node_modules/mysql/lib/protocol/Protocol.js:398:10)
- react-server | at Protocol.end (/app/server/node_modules/mysql/lib/protocol/Protocol.js:116:8)
- react-server | at Socket.<anonymous> (/app/server/node_modules/mysql/lib/Connection.js:94:28)
- react-server | [... lines matching original stack trace ...]
- react-server | at processTicksAndRejections (node:internal/process/task_queues:83:21) {
- react-server | fatal: true,
- react-server | code: 'PROTOCOL_CONNECTION_LOST'
- react-server | }
- react-server exited with code 1
复制代码
在做数据库相关查询时间,遇到PROTOCOL_CONNECTION_LOST问题,虽然做了控制,但debug log还是会出现,以我知解决方法有两个,
一个是出现code: PROTOCOL_CONNECTION_LOST后重连
第二个是使用connection pool处理,下面是demo
- var mysql = require("mysql");
- var pool = mysql.createPool({
- host: 'your host',
- user:'your account',
- password:'your password',
- database:'your database',
- connectionLimit : 10,
- });
复制代码
查询数据库,重点conn.query后一定要conn.release()
- var query = function(sql, options, callback) {
- pool.getConnection(function(err, conn){
- if (err) {
- callback(err, null, null);
- } else {
- conn.query(sql, options, function(err, results, fields){
- });
- // release connection。
- conn.release();
- }
- });
- };
复制代码
|
|