Sails.js + Postgres:交易问题 [英] Sails.js + Postgres: issue with transactions

查看:99
本文介绍了Sails.js + Postgres:交易问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Postgres作为数据库在Sails 0.10.5中实现事务,但是最后未提交(或回滚)操作。

I am trying to implement a transaction in Sails 0.10.5 using Postgres as database, but the operations are not committed (or rolled back) at the end.

这是我作为测试记录下来的一个简单的事务场景(使用async.js):

This is a simple transaction scenario I wrote down as a test (using async.js):

testTransaction: function(uri) {

    var testFile = {
        uri: uri,
        details: { 'firstDetail': 'value' }
    };

    async.auto({
        begin_transaction: function(callback) {
            DataFile.query('BEGIN TRANSACTION;', callback);
        },
        new_data_file: ['begin_transaction', function(callback) {
            DataFile.create(testFile).exec(callback);
        }],
    }, function(error, results) {
        if (error) {
            console.log(error.message);
            DataFile.query('ROLLBACK;', function(e, r) {
                return error.message;
            });
            return;
        }
        DataFile.query('COMMIT;', function(e, r) {
            console.log("Saved file: " + results.new_data_file);
            if (e) {
                return "Error during commit";
            }
            return results.new_data_file;
        });
    });

}

然后我运行该服务时没有错误;但不会将新的DataFile enetity添加到Postgres表中。如果我查看Postgres日志,就会发现:

Then I run the service without errors; but no new DataFile enetity is added to the Postgres table. If I check the Postgres log, I find out that:

2014-12-04 10:35:01 GMT 7984 548038d0.1f30LOG:  statement: BEGIN TRANSACTION;
2014-12-04 10:35:01 GMT 7977 548038d0.1f29LOG:  execute <unnamed>: INSERT INTO "data_file" ("uri", "details", "created_at", "updated_at") values ($1, $2, $3, $4) RETURNING *
2014-12-04 10:35:01 GMT 7977 548038d0.1f29DETAIL:  parameters: $1 = '/just/another/test/uri', $2 = '{"firstDetail":"value"}', $3 = '2014-12-04 10:35:01+00', $4 = '2014-12-04 10:35:01+00'
2014-12-04 10:35:01 GMT 7983 548038d0.1f2fLOG:  statement: COMMIT;
2014-12-04 10:35:01 GMT 7983 548038d0.1f2fWARNING:  there is no transaction in progress

因此我得到警告:没有正在进行的事务
日志中的第三个元素是进程ID。
显然,COMMIT语句是通过与发出BEGIN的过程(7984)不同的过程(7983)发出的。可能是这个问题吗?在Sails中处理交易时,如何强制使用相同的流程?

So I get the warning: "there is no transaction in progress" The third element on the log is the Process ID. Apparently the COMMIT statement is issued by a different process (7983) than the one that issued the BEGIN (7984). Might this be the issue?. How can I force to use the same process when dealing with transactions in Sails?

推荐答案

事务依赖于使用相同连接进行的多个查询,但是Waterline对每个查询使用不同的连接,这就是为什么您将收到没有正在进行的交易消息-第二个查询正在使用不同的连接,该连接没有正在进行的交易。

Transactions rely on several queries being made with the same connection, but Waterline uses a different connection for every query, which is why you're getting the "there is no transaction in progress" message - the second query is using a different connection, which doesn't have an in-progress transaction.

除了将 poolSize 设置为1(因此每个查询必须使用相同的连接)之外,没有其他方法可以解决。

There's no way to work around this, other than to set the poolSize to 1 (so every query must use the same connection).

这是我们用于交易的库- https://github.com/Shyp/pg-交易。您将无权访问辅助方法- .find() .update()等,但至少可能。

Here's the library we use for transactions - https://github.com/Shyp/pg-transactions. You won't have access to the helper methods - .find(), .update() etc but at least it's possible.

这篇关于Sails.js + Postgres:交易问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆