美文网首页
nodejs使用mysql事务

nodejs使用mysql事务

作者: 手持赤旗 | 来源:发表于2019-10-30 20:55 被阅读0次

谨慎使用事务,不然程序跑不动还不知道哪里出错了,直接贴上代码

class ConnectMysql{
    constructor(options) {
        this.Pool = null
        this.options = options
        this.options.charset = options.charset || 'utf8mb4'
        this.options.connectionLimit = options.connectionLimit || 100
        this.options.queueLimit      = options.queueLimit || 10000
        this.options.multipleStatements      = options.multipleStatements || true
        this.ConnectDB()
    }
    ConnectDB() {
        try {
            this.Pool = mysql.createPool(this.options)
        } catch (error) {
            throw new Error("connect DB error!!")
        }
    }
    transactions() {
        var self = this;
        if(!self.Pool) {
            self.ConnectDB();
        }
        if(self.Pool._close) {
            throw Error("db connection Pool closed");
        }
        return new Promise((resolve, reject) => {
            self.Pool.getConnection(function(err, connection) {
                if(err) {
                    reject(err)
                    return
                }
                if(!connection) {
                    console.error("sorry connect database fail!!")
                    reject(err)
                    return
                }
                
                connection.beginTransaction(function(err) {
                    if (err) {
                        console.error("beginTransaction fail!!")
                        reject(err)
                    }
                    connection.exc_query = function(sql) {
                        return new Promise((resolve, reject) => {
                            connection.query(sql, (error, results, fields) => {
                                if(error) {
                                    reject(error)
                                }
                                resolve(self.convertRows(results))
                            })
                        })
                    }
                    resolve(connection)
                  })
            })
        })
    }
    convertRows(rows) {
        if(!(rows instanceof Array)) {
            return [rows]
        }

        if(rows[0] && (rows[rows.length -1] instanceof Array)) {
            return rows[rows.length - 1]
        }
        return rows
    }
}

相关文章

  • nodejs使用mysql事务

    谨慎使用事务,不然程序跑不动还不知道哪里出错了,直接贴上代码

  • 事务之实际例子(废弃)

    1、前言 平时开发我们经常使用 Spring 事务,而 Spring 默认使用 mysql 的事务。mysql 事...

  • 命令行程-开发

    nodejs命令行程序开发 事务流程 使用参数 使用进程

  • The solution is: Error: ER_NOT_

    问题描述:nodejs 使用mysql 库连接Mac上的MySQL时,报错 解决办法:登录进mysql

  • nodejs 使用 mysql

    相比与java,nodejs的mysql连接方式较为简单,但需要注意其异步特性 1. 导入mysql模块 建立my...

  • nodejs使用mysql

    1、下载mysql模块 2、引入 const mysql = require('mysql'); 3、连接数据库 ...

  • MySQL白菜教程(Level 8)

    MySQL Transaction MySQL 事务以及如何使用 COMMIT 和 ROLLBACK 语句来管理 ...

  • 事物处理

    MySQL 事务 在 MySQL 中只有使用了 Innodb 数据库引擎的数据库或表才支持事务。 事务处理可以...

  • 试着使用nodejs操作mysql

    如果要在nodejs中使用mysql我们必须要借助mysql模块因此我们必须要用npm下载mysql模块 然后我们...

  • 10-事务

    事务基础 什么是事务MySQL 中的事务主要用于处理操作量大,复杂度高的数据。MySQL 中只有使用了 Innod...

网友评论

      本文标题:nodejs使用mysql事务

      本文链接:https://www.haomeiwen.com/subject/lwpavctx.html