nodejs 事务问题
发布于 5个月前 作者 zyoo 774 次浏览

代码如下:

var mysql = require('mysql');                                                                         
var conn = mysql.createConnection({                                                                   
    host:'localhost',                                                                                 
    user:'root',                                                                                      
    password:'',
    database:'cluster_manager_dev',
    port:3306
});                                                                                                   
conn.beginTransaction(function(err) {                                                                 
        if(err) {
                throw err;
                console.log(err);
        }
        conn.query('insert into user set user_name = ?, password = ?, is_admin = ?', ['testkjk', '123456', '2'], function(err, result) {
                if(err) {
                        conn.rollback(function() {                                                    
                                console.log('first insert into user');                                
                                throw err;                                                            
                        });
                }
                conn.query('insert into user', function(err, result) {
                        if(err) {
                                console.log('second insert into user');                               
                                conn.rollback(function() {                                            
                                        throw err;                                                    
                                });                                                                   
                        }
                        conn.commit(function(err) {                                                   
                                console.log('enter the commit');                                      
                                if(err) {
                                        conn.rollback(function() {                                    
                                                throw err;                                            
                                        });                                                           
                                }
                                console.log('success');
                        });
                });
        });
});

最终的结果第一条语句执行了,向数据库中插入了testkjk,而且程序也打印了 second insert into user, 而没有回滚,这是为什么呢?

附上程序执行错误输出:

second insert into user
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
    at Query.Sequence._packetToError (/usr/local/NArk/web/node_modules/mysql/lib/protocol/sequences/Sequence.js:32:14)
    at Query.ErrorPacket (/usr/local/NArk/web/node_modules/mysql/lib/protocol/sequences/Query.js:82:18)
    at Protocol._parsePacket (/usr/local/NArk/web/node_modules/mysql/lib/protocol/Protocol.js:177:24)
    at Parser.write (/usr/local/NArk/web/node_modules/mysql/lib/protocol/Parser.js:62:12)
    at Protocol.write (/usr/local/NArk/web/node_modules/mysql/lib/protocol/Protocol.js:37:16)
    at Socket.ondata (stream.js:51:26)
    at Socket.emit (events.js:117:20)
    at Socket.<anonymous> (_stream_readable.js:748:14)
    at Socket.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:410:10)
    --------------------
    at Query.Sequence (/usr/local/NArk/web/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:20)
    at new Query (/usr/local/NArk/web/node_modules/mysql/lib/protocol/sequences/Query.js:12:12)
    at Function.Connection.createQuery (/usr/local/NArk/web/node_modules/mysql/lib/Connection.js:47:10)
    at Connection.query (/usr/local/NArk/web/node_modules/mysql/lib/Connection.js:131:26)
    at Query._callback (/usr/local/NArk/web/test.js:24:8)
    at Query.Sequence.end (/usr/local/NArk/web/node_modules/mysql/lib/protocol/sequences/Sequence.js:75:24)
    at Query._handleFinalResultPacket (/usr/local/NArk/web/node_modules/mysql/lib/protocol/sequences/Query.js:143:8)
    at Query.OkPacket (/usr/local/NArk/web/node_modules/mysql/lib/protocol/sequences/Query.js:77:10)
    at Protocol._parsePacket (/usr/local/NArk/web/node_modules/mysql/lib/protocol/Protocol.js:177:24)
    at Parser.write (/usr/local/NArk/web/node_modules/mysql/lib/protocol/Parser.js:62:12)
8 回复
if( err ) { ..... ;return; }

@PerterPon 亲,没懂什么意思? 加上return就可以了?

rollback之后要return,不然后面的语句会继续执行

事务处理建议使用 bearcat-dao 吧,这样子太原始了,业务逻辑和事务代码耦合在一起

@ravenwang 亲 不加return确实会执行后面的语句,我加了return之后,第一条插入sql还是执行了,官方给的例子上页木有return,疑惑啊 附上链接:https://github.com/felixge/node-mysql#transactions

@fantasyni 非常感谢 我看看这个哈

问题解决了,没设置innoDB,太2了。。。

回到顶部