事务的 mongoc 文档使用会话。所以我尝试在与会话关联的事务中插入文档。但我得到了错误
[ 3666]: ERROR: mongoc: Failed to start session: Server does not support sessions
如何使用带有 FoundationDB 文档层的 MongoDB C 驱动程序进行事务?
我目前的代码是:
- (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;
}
// ...
}