首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >节点MySQL池连接中的事务处理

问节点MySQL池连接中的事务处理
EN

Stack Overflow用户
提问于 2019-09-28 17:43:57
回答 1查看 89关注 0票数 1

我已经对我的mysql连接进行了改进,以便我可以在我的sql连接中使用诺言链接。

代码语言:javascript
复制
var pool = mysql.createPool({
    connectionLimit: 50,
    host: keys.connection.host,
    multipleStatements: true,
    user: keys.connection.user,
    password: keys.connection.password,
    database: keys.connection.database,
    dateStrings: true 
    // debug:true                //Set this to true for verbose debugging. Leaving this to default for now cause it is creating too many messages at my console
})
pool.getConnection((err, connection) => {
    if (err) {
        if (err.code === 'PROTOCOL_CONNECTION_LOST') {
            console.error('Database connection was closed.')
        }
        if (err.code === 'ER_CON_COUNT_ERROR') {
            console.error('Database has too many connections.')
        }
        if (err.code === 'ECONNREFUSED') {
            console.error('Database connection was refused.')
        }
    }
    if (connection) connection.release()
    return
})

pool.query = util.promisify(pool.query)

我想在数据库中创建一些表,如果其中一个查询失败,则不执行任何查询。

代码语言:javascript
复制
async function UpdateSchema_1_0_1() {
    try {

        await pool.query(`CREATE TABLE \`printer\`(
                        \`ID\` int(10) NOT NULL,
                        \`Station\` varchar(255) NOT NULL,
                        \`Printer\` varchar(255) NOT NULL,
                        \`XML\` varchar(6000) NOT NULL,
                        PRIMARY KEY (ID)
                    )`)

        await pool.query(`CREATE TABLE \`printerqueue\`(
            \`PrinterID\` int(10) NOT NULL,
            \`OrderNumber\` varchar(255) NOT NULL,
            FOREIGN KEY (PrinterID) REFERENCES printer(ID)
            );`)

        await pool.query(`Alter table users add column printerID int(10) Default 1;`)

        await pool.query(`ALTER TABLE users ADD CONSTRAINT fk_users_printer FOREIGN KEY (printerID) REFERENCES printer(ID);`)

    } catch (err) {
        console.log(err)
    }
}

是否有一种方法可以在我已编程的mysql池中使用提交和回滚事务?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-28 18:31:54

无论如何,都无法回滚、创建表或修改表语句。这与连接池或Node.js无关。在MySQL中,每个DDL (数据定义语言)语句都会导致隐式提交。

详细信息请阅读https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html。

因此,无法将一组DDL语句作为一个原子组执行(全部成功,否则就会回滚到目前为止所做的工作)。至少不使用原子事务的特性。

如果其中一条DDL语句失败,并且希望“回滚”以将数据库返回到启动DDL语句组之前的状态,则必须手动逆转任何以前的DDL语句。

当然,某些类型的DDL更改无法逆转,除非通过从备份还原数据库,因为这些更改会破坏数据。就像扔桌子或者换桌子..。丢栏。要逆转这些更改并恢复该表或列中的数据,您必须拥有原始数据。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58148851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档