type here
bool
create_and_insert_doc (mongoc_client_session_t *session,
void *ctx,
bson_t **reply, /* out param for our server reply */
bson_error_t *error)
{
/*
* mongoc_collection_insert_one requires an uninitialized, stack-allocated
* bson_t to receive the update result
*/
bson_t local_reply;
bson_t *doc = NULL;
ctx_t *data = NULL;
bool retval;
for (int i = 0; i < 1000; i++) {
doc = BCON_NEW ("new", BCON_INT32 (2), "val", BCON_INT32 (2));
printf ("Running the user-defined callback in a newly created transaction...n");
data = (ctx_t *) ctx;
retval = mongoc_collection_insert_one (data->collection, doc, data->insert_opts, &local_reply, error);
{
sleep(0.5);
}
}
printf ("sleep end...n");
/*
* To return to the mongoc_client_session_with_transaction() method, set
* *reply to a new copy of our local_reply before destroying it.
*/
*reply = bson_copy (&local_reply);
bson_destroy (&local_reply);
bson_destroy (doc);
return retval;
}
mongoc_client_session_with_transaction (session, &create_and_insert_doc, NULL, &ctx, &reply, &error);
When I make Transaction using mongoc driver above. And run the code which runs about 5 seconds just inserting documents.
Other operation like insert, update, create index are not blocked. why is that?
I know mongodb use intention lock, Shared lock, exclusive lock. So If I make transaction which just insert single documents. intent exclusive lock is obtained for database and collection. exclusive lock for the document.
I think I am missing or misunderstanding something. Thanks for reading.
I ran test code. checked db.currentOp(). ran insert, update, create index while running tranjection.
Insub is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.