gpt4 book ai didi

c - 如何使用 MongoDB C 驱动程序和 FoundationDB 文档层进行事务处理?

转载 作者:行者123 更新时间:2023-11-30 14:37:30 26 4
gpt4 key购买 nike

交易的 mongoc 文档使用 session 。因此,我尝试在与 session 关联的事务中插入文档。但我收到了错误

[ 3666]:    ERROR:       mongoc: Failed to start session: Server does not support sessions

如何使用 MongoDB C 驱动程序和 FoundationDB 文档层进行事务处理?

我当前的代码是:

- (int)insertUser:(User *)user {
mongoc_client_session_t *session;
mongoc_session_opt_t *session_opts;
mongoc_transaction_opt_t *default_txn_opts;
mongoc_transaction_opt_t *txn_opts;
mongoc_read_concern_t *read_concern;
mongoc_write_concern_t *write_concern;
bson_t *doc;
bson_t *insert_opts;
bson_error_t err;
default_txn_opts = mongoc_transaction_opts_new();
read_concern = mongoc_read_concern_new();
mongoc_read_concern_set_level(read_concern, "snapshot");
mongoc_transaction_opts_set_read_concern(default_txn_opts, read_concern);
session_opts = mongoc_session_opts_new();
mongoc_session_opts_set_default_transaction_opts(session_opts, default_txn_opts);
session = mongoc_client_start_session(client, session_opts, &err);
if (!session) {
MONGOC_ERROR("Failed to start session: %s", err.message);
return EXIT_FAILURE;
}
txn_opts = mongoc_transaction_opts_new();
write_concern = mongoc_write_concern_new();
mongoc_write_concern_set_wmajority(write_concern, 1000); // write timeout
mongoc_transaction_opts_set_write_concern(txn_opts, write_concern);
}
int insertUserTransaction(id param, mongoc_client_session_t *session, mongoc_transaction_opt_t *txn_opts, User *user, bson_error_t *err) {
bool ret = mongoc_client_session_start_transaction(session, txn_opts, err);
if (!ret) {
MONGOC_ERROR("Failed to start transaction: %s", err->message);
return EXIT_FAILURE;
}
// ...
}

最佳答案

文档层尚不支持事务。它用于通过一些与 MongoDB 构造不同的构造来公开 FoundationDB 事务。但它们会被禁用一段时间。

关于c - 如何使用 MongoDB C 驱动程序和 FoundationDB 文档层进行事务处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57278097/

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