gpt4 book ai didi

transactions - bluebird node-mysql2事务

转载 作者:行者123 更新时间:2023-12-04 18:00:44 26 4
gpt4 key购买 nike

一直在尝试 promisify node-mysql2 事务,但无法让它工作,有什么指示吗?

我已经阅读了 http://bluebirdjs.com/docs/api/disposer.html 上的文档

我也有非交易相关代码的工作版本,但是让交易正常工作,以及适当的资源处置和错误处理一直很困难。这是从 node-mysql api 文档中获取的基于回调的实现

connection.beginTransaction(function(err) {
if (err) { throw err; }
connection.query('INSERT INTO posts SET title=?', title, function(err, result) {
if (err) {
return connection.rollback(function() {
throw err;
});
}

var log = 'Post ' + result.insertId + ' added';

connection.query('INSERT INTO log SET data=?', log, function(err, result) {
if (err) {
return connection.rollback(function() {
throw err;
});
}
connection.commit(function(err) {
if (err) {
return connection.rollback(function() {
throw err;
});
}
console.log('success!');
});
});
});
});

这是我对查询管理的实现

import { mysqldb } from '../config';
import { createPool } from 'mysql2';
import Pool from 'mysql2/lib/pool';
import Connection from 'mysql2/lib/connection';
import { using, promisifyAll, try as promiseTry } from 'bluebird';

promisifyAll([Pool, Connection]);
const pool = createPool(mysqldb);

export const getConnection = () =>
pool.getConnectionAsync().disposer(connection =>
connection.release());

export const withTransaction = (fn) =>
using(getConnection(), tx =>
promiseTry(fn(tx), tx.beginTransaction()).then(
res => tx.commitAsync().catch(e => e).thenReturn(res),
err => tx.rollbackAsync().catch(e => e).thenThrow(err)
)
);

// withTransaction(tx =>
// tx.executeAsync('sql1')
// .then(tx.executeAsync('sql2'))
// .then(tx.executeAsync('sql3'))
// .then(tx.executeAsync('sql4'))
// );

export const query = (sql, params) =>
using(getConnection(), connection =>
connection.executeAsync(sql, params)
);

然而交易给了我

Unhandled rejection TypeError: expecting a function but got [object Object]
at apiRejection (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise.js:10:27)
at Promise.attempt.Promise.try (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/method.js:26:16)
at /Users/willh/workspace/ChenPin/graphql/lib/services/mysql.js:35:30
at tryCatcher (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/util.js:16:23)
at /Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/using.js:184:26
at tryCatcher (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise.js:503:31)
at Promise._settlePromise (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise.js:560:18)
at Promise._settlePromise0 (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise.js:605:10)
at Promise._settlePromises (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise.js:684:18)
at Promise._fulfill (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise.js:629:18)
at PromiseArray._resolve (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise_array.js:125:19)
at PromiseArray._promiseFulfilled (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise_array.js:143:14)
at Promise._settlePromise (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise.js:565:26)
at Promise._settlePromise0 (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise.js:605:10)
at Promise._settlePromises (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise.js:684:18)
at Promise._fulfill (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise.js:629:18)
at Promise._resolveCallback (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise.js:424:57)
at Promise._settlePromiseFromHandler (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise.js:515:17)
at Promise._settlePromise (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise.js:560:18)
at Promise._settlePromise0 (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise.js:605:10)
at Promise._settlePromises (/Users/willh/workspace/ChenPin/graphql/node_modules/bluebird/js/release/promise.js:684:18)

什么是正确的方法?

更新:

通过查看源代码让它工作,结果发现它只是一个简单的助手,真的不值得使用他们的助手,下面是最终的工作版本。无论如何感谢您的帮助:D

export const withTransaction = (fn) =>
using(getConnection(), tx =>
tx.queryAsync('START TRANSACTION').then(fn(tx)).then(
res => tx.queryAsync('COMMIT').thenReturn(res),
err => tx.queryAsync('ROLLBACK').catch(e => e).thenThrow(err)
)
);

// withTransaction(tx =>
// tx.executeAsync('select :x + :y as z', { x: 1, y: 2 }).then(res1 =>
// tx.executeAsync('select :x + :y as z', { x: res1[0].z, y: 1 })).then(res2 =>
// tx.executeAsync('select :x + :y as z', { x: res2[0].z, y: 1 })).then(res3 =>
// tx.executeAsync('select :x + :y as z', { x: res3[0].z, y: 1 })).then(res4 => res4)
// );

顺便说一句,原来的 promisify 方法是有效的,没有什么区别,但感谢您帮助我!

最佳答案

您遇到错误是因为 promisifyAll 使用的是函数而不是对象。在大多数情况下,函数存在于类的 prototype 中。试试这个

var Promise = require('bluebird');
var mysql = require('mysql2');
Promise.promisifyAll(mysql.Connection.prototype);
Promise.promisifyAll(require('mysql2/lib/pool').prototype)

在我上面的练习示例中,效果很好。

如果你觉得你想在这个领域做更多的实验,你也可以手动遍历所有元素并只选择函数,就像我个人在 Postgres 中使用的下面的例子

var Promise = require('bluebird');
var pg = require('pg');
Object.keys(pg).forEach(function (key) {
var Cls = null;
try {
Cls = pg[key];
if (typeof Cls === 'function') {
Promise.promisifyAll(Cls.prototype);
Promise.promisifyAll(Cls);
}
} catch (e) {
console.log(e);
}
});
Promise.promisifyAll(pg);

关于transactions - bluebird node-mysql2事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35758045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com