问题描述
我想对多个查询和承诺使用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_work.commit();//until here, no problem
G_work.exec("insert into test.table1(nom) VALUES('bar');"); //error, transaction already closed
G_work.commit();
}
当我尝试插入'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::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_work.commit();
} 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重用/重新激活工作事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!