pqxx重用/重新激活工作事务

编程入门 行业动态 更新时间:2024-10-26 16:22:26
本文介绍了pqxx重用/重新激活工作事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想对多个查询和承诺使用pqxx :: work,而 commit 功能阻止我再次使用它。 下面是一个简单的例子:

I want to use a pqxx::work for multiples queries AND commitments, while the commit function prevent me from using it again. Here is a simple example :

pqxx::connection G_connexion("dbname=basetest user=usertest password=1234"); pqxx::work G_work(G_connexion); int main(int argc, char* argv[]) { G_work.exec("insert into test.table1(nom) VALUES('foo');"); G_workmit();//until here, no problem G_work.exec("insert into test.table1(nom) VALUES('bar');"); //error, transaction already closed G_workmit(); }

当我尝试插入'bar'值时, get a pqxx :: usage_error:执行查询时出错。尝试激活事务< READ COMMITTED>已关闭

When I try to insert the 'bar' value, after the commit, I get a pqxx::usage_error : Error executing query . Attempt to activate transaction<READ COMMITTED> which is already closed

如何在提交更改后避免关闭连接?我可以重置G_work成功相当于G_work = pqxx ::工作(G_connexion),或其他? 另外,一个错误的请求不应该崩溃整个过程,只是一个正在进行中(G_work仍然可以使用失败后)。

How can I avoid to close the connection after I commit the changes ? can i reset G_work with a successing equivalent of G_work=pqxx::work(G_connexion), or other ? Also, one bad request should not crash the entire process, just the one in process (G_work still usable after a failure).

我必须保持相同的变量G_Work,因为它将是从程序中许多地方调用的全局变量。

I have to keep the same variable G_Work because it will be a global variable called from lots of places in the program.

推荐答案

pqxx :: work 只是一个 pqxx :: transaction<> ,它最终从 pqxx :: transaction_base 。

pqxx::work is just a pqxx::transaction<> which eventually gets most of its logic from pqxx::transaction_base.

此类不适用于多个事务。相反,它用于try / catch块中的单个事务。它有一个状态成员变量( m_Status ),即使在提交后也不会重新初始化。

This class is not intended to serve for several transactions. Instead, it is intended for a single transaction within a try/catch block. It has a state member variable (m_Status) which is never reinitialized, even after a commit.

正常模式是:

{ pqxx::work l_work(G_connexion); try { l_work.exec("insert into test.table1(nom) VALUES('foo');"); l_workmit(); } catch (const exception& e) { l_work.abort(); throw; } }

可以说,libpqxx可以在删除时回滚事务避免尝试/ catch完全)但它不。

Arguably, libpqxx could rollback the transaction on deletion (to avoid the try/catch entirely) but it doesn't.

似乎这不适合你的使用模式,因为你想要 G_work 是一个全局变量,可从程序中的多个地方访问。请注意,pqxx :: work不是连接对象的类,而只是一种用C ++异常处理来封装begin / commit / rollback的方法。

It seems that this doesn't fit your usage pattern as you want G_work to be a global variable accessible from several places in your program. Please note that pqxx::work is not the class for connection objects, but just a way to encapsulate begin/commit/rollback with C++ exceptions handling.

然而,libpqxx允许您执行语句外部事务(或至少在libpqxx管理的事务外)。您应该使用 pqxx :: nontransaction 类的实例。

Nevertheless, libpqxx also allows you to execute statement outside transactions (or at least outside libpqxx-managed transactions). You should use instances of pqxx::nontransaction class.

#include "pqxx/nontransaction" pqxx::connection G_connexion("dbname=basetest user=usertest password=1234"); pqxx::nontransaction G_work(G_connexion); int f() { G_work.exec("insert into test.table1(nom) VALUES('foo');"); G_work.exec("insert into test.table1(nom) VALUES('bar');"); }

请注意,这相当于:

#include "pqxx/nontransaction" pqxx::connection G_connexion("dbname=basetest user=usertest password=1234"); int f() { pqxx::nontransaction l_work(G_connexion); l_work.exec("insert into test.table1(nom) VALUES('foo');"); l_work.exec("insert into test.table1(nom) VALUES('bar');"); }

最后,无法阻止您管理交易 pqxx :: nontransaction 。如果您想要保存点,则尤其如此。我还建议使用 pqxx :: nontransaction 如果你的事务意味着超过一个函数范围(例如在全局范围)。

Eventually, nothing prevents you to manage transactions with pqxx::nontransaction. This is especially true if you want savepoints. I would also advise using pqxx::nontransaction if your transaction is meant to last beyond a function scope (e.g. at global scope).

#include "pqxx/nontransaction" pqxx::connection G_connexion("dbname=basetest user=usertest password=1234"); pqxx::nontransaction G_work(G_connexion); int f() { G_work.exec("begin;"); G_work.exec("insert into test.table1(nom) VALUES('foo');"); G_work.exec("savepoint f_savepoint;"); // If the statement fails, rollback to checkpoint. try { G_work.exec("insert into test.table1(nom) VALUES('bar');"); } catch (const pqxx::sql_error& e) { G_work.exec("rollback to savepoint f_savepoint;"); } G_work.exec("commit;"); }

更多推荐

pqxx重用/重新激活工作事务

本文发布于:2023-10-26 09:22:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529751.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事务   工作   pqxx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!