Fix withTransaction Sample Code

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Fixed
    • Priority: Unknown
    • 2.3.0
    • Affects Version/s: None
    • Component/s: None
    • None
    • Not Needed
    • None
    • Fully Compatible
    • C Drivers
    • Needed
    • Hide

      1. What would you like to communicate to the user about this feature? In order for a write operation to be part of a transaction, you must explicitly pass the session ID via the opts argument.
      2. Would you like the user to see examples of the syntax and/or executable code and its output? Yes, the purpose of this task was to fix example code that is currently shown to users.
      3. Which versions of the driver/connector does this apply to? Versions 1.15 and above.

      Show
      1. What would you like to communicate to the user about this feature?  In order for a write operation to be part of a transaction, you must explicitly pass the session ID via the opts argument. 2. Would you like the user to see examples of the syntax and/or executable code and its output?  Yes, the purpose of this task was to fix example code that is currently shown to users. 3. Which versions of the driver/connector does this apply to? Versions 1.15 and above.
    • None
    • None
    • None
    • None
    • None
    • None

      The sample code for withTransaction does not set an explicit session for the calls to mongoc_collection_insert_one. This means these write operations are not actually part of the transaction. To associate these write operations with the transaction, the session ID should be passed as part of the opts argument.

      Additionally, the sample code passes the reply argument to the mongoc_collection_insert_one calls, but that is not the intended use of the parameter. The bson_t that reply points to is NULL, then the callback can choose to construct a bson_t if propagating a reply is desired. To be clear, this code isn't incorrect, but passing *reply to write operations when it is actually NULL is misleading. In most cases, passing NULL explicitly as the reply is suitable.

      All in all, sample code that looks like this:

      ret = mongoc_collection_insert_one(coll, doc, NULL, *reply, error); 

      should be amended to look like this:

      bson_t *opts = bson_new();
      BSON_ASSERT(mongoc_client_session_append(session, opts, error));
      
      ret = mongoc_collection_insert_one(coll, doc, opts, NULL, error);
      
      bson_free(opts);

            Assignee:
            Connor MacDonald
            Reporter:
            Connor MacDonald
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: