The mongoc documentation for transaction uses session. So I tried to insert documents within a transaction associated with a session. But I got the error
[ 3666]: ERROR: mongoc: Failed to start session: Server does not support sessions
How to do transactions using MongoDB C driver with FoundationDB Document Layer?
My current code is:
- (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;
}
// ...
}
Document layer doesn’t support transactions, yet. It used to expose FoundationDB transactions through some constructs different to MongoDB constructs. But they are disabled for a while.