0

我已经开始使用池连接到 mysql,而不是使用与Node.js的普通连接。因为我需要在进程完成后释放连接。当连接保持打开时,由于mysql超时问题, Node.js进入异常。

我已经编辑了我的代码,但它没有在第 94 行给我这样的错误:

TypeError: Object #<Connection> has no method 'release'
    at Query._callback (/usr/local/src/conn.js:94:28)

在那条线之上:

socket.on('getUnreadPM', function (msg) {


    var userSocket = clients[msg.userDestID];

    if (msg.lastGotMessage == 'none') {
        post = 'Select * from messages where (destUserID=\'' + msg.userDestID + '\' AND srcUserID=\'' + msg.userSrcID + '\') OR (destUserID=\'' + msg.userSrcID + '\' AND srcUserID=\'' + msg.userDestID + '\')';

    } else {

        post = 'Select * from messages where destUserID=\'' + msg.userDestID + '\' AND srcUserID= \'' + msg.userSrcID + '\' AND messageSendDate between \'' + msg.lastGotMessage + '\' and \'' + msg.deviceCurrentTime + '\' AND messageSendDate > \'' + msg.lastGotMessage + '\' order by messageSendDate';

    }

    pool.getConnection(function (err, connection) {
        // Use the connection   
        connection.query(post, function (error, results, fields) {
            // And done with the connection.

            if (results.length > 0) {

                if (typeof userSocket !== 'undefined') {

                    var boolData = results[0];
                    //console.log('bool data lenght : ',results.length);
                    var messageID = new Array();
                    var srcUserID = new Array();
                    var destUserID = new Array();
                    var messageContent = new Array();
                    var messageDate = new Array();
                    var didRead = new Array();

                    for (var i = 0; i < results.length; i++)

                    {
                        messageID.push(results[i].messageID);
                        srcUserID.push(results[i].srcUserID);
                        destUserID.push(results[i].destUserID);
                        messageContent.push(results[i].messageContent);
                        messageDate.push(dateFormat(results[i].messageSendDate, "yyyy-mm-dd HH:MM:ss"));
                        didRead.push(results[i].didRead);
                    }

                    userSocket.emit('history :', {
                        'dataMode': 'history',
                            'srcUserID': srcUserID,
                            'destUserID': destUserID,
                            'messageContent': messageContent,
                            'messageSendDate': messageDate,
                            'didread': didRead
                    });

                } else {

                    userSocket.emit('history :', {
                        'dataMode': 'none',

                    });
                }

            } else {

                userSocket.emit('history :', {
                    'dataMode': 'none',

                });                

            }

            connection.release();

        });
    });
});
4

1 回答 1

0

mysql连接没有释放方法。所以你不能释放它。

几个月前我已经为我的应用程序编写了这样一个节点模块,你可以在这里找到它:https ://github.com/LinuxDoku/node-mysql-pool

它在启动时创建一个连接池,将您的查询排队并将它们连续发送到数据库。获得结果后,将释放连接以进行新的查询。

于 2013-09-05T12:30:20.207 回答