title: Why do I get Error resulting in state DEAD in MongoDB?
body: <p>I’m working with multidocument mongodb transactions, and I’m getting an unexpected error.</p>
  
  <p>In my c++ app, using mongocxx driver, the steps I’m performing are the following:</p>
  
  <ol>
  <li>create session</li>
  <li>start transaction</li>
  <li>create a bulk_write 1 for this session, where I have multiple
  updates (~1000) to colletion 1</li>
  <li>create another bulk_write 2 for this session, where I have
  multiple inserts (~1000) to collection 2</li>
  <li>execute bulk_write 1</li>
  <li>execute bulk write 2</li>
  <li>commit transaction</li>
  </ol>
  
  <p>This is the code snippet for the algorithm:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  #include &lt;bsoncxx/builder/stream/document.hpp&gt;
  #include &lt;bsoncxx/json.hpp&gt;
  #include &lt;bsoncxx/exception/exception.hpp&gt;
  #include &lt;mongocxx/client.hpp&gt;
  #include &lt;mongocxx/instance.hpp&gt;
  #include &lt;mongocxx/exception/exception.hpp&gt;
  #include &lt;mongocxx/exception/logic_error.hpp&gt;
  #include &lt;mongocxx/exception/operation_exception.hpp&gt;
  
  
  
  int main(int argc, char** argv)
  {
      /* Parameters */
  
      std::string db_uri = "&lt;PROVIDE URI TO CONNECT WITH A MONGO DB WITH REPLICA SETS&gt;";
      std::string db_name = "db_0";
      std::string collection0_name = "coll_0";
      std::string collection1_name = "coll_1";
      int N_UPDATES = 100000;
      int N_INSERTS = 100000;
  
      /* Init connection */
  
      static mongocxx::instance inst{};
      mongocxx::uri client_uri = mongocxx::uri(db_uri);
      mongocxx::options::client client_options;
      mongocxx::options::ssl ssl_options;
      ssl_options.allow_invalid_certificates(true);
      client_options.ssl_opts(ssl_options);
      mongocxx::client client = mongocxx::client(client_uri, client_options);
  
      /* Reinit collections */
  
      mongocxx::database db = client[db_name];        
      auto builder = bsoncxx::builder::stream::document{};
      bsoncxx::document::value doc_value = builder
      &lt;&lt; "Hello" &lt;&lt; "MongoDB"
      &lt;&lt; bsoncxx::builder::stream::finalize;    
      db[collection0_name].insert_one(doc_value.view()); /* insert a dummy doc */    
      db[collection0_name].delete_many({}); /* delete all docs */
      db[collection1_name].insert_one(doc_value.view()); /* insert a dummy doc */    
      db[collection1_name].delete_many({}); /* delete all docs */
  
      /* Create session */
  
      mongocxx::client_session session = client.start_session();
  
      /* Start transaction */
  
      session.start_transaction();
  
      /* Create bulk operations */
  
      mongocxx::bulk_write op0 = db[collection0_name].create_bulk_write(session);
      mongocxx::bulk_write op1 = db[collection1_name].create_bulk_write(session);
      std::vector&lt;mongocxx::bulk_write*&gt; bulk_operations;
      bulk_operations.push_back(&amp;op0);
      bulk_operations.push_back(&amp;op1);
  
      /* Fill update bulk operations */
  
      for (int i = 0; i &lt; N_UPDATES; i++){
  
          mongocxx::model::update_one upsert_one{
              bsoncxx::builder::basic::make_document(
                  bsoncxx::builder::basic::kvp("field0", i),
                  bsoncxx::builder::basic::kvp("field1", i)
              ),
              bsoncxx::builder::basic::make_document(
                  bsoncxx::builder::basic::kvp("$set", bsoncxx::builder::basic::make_document(
                      bsoncxx::builder::basic::kvp("field0", i),
                      bsoncxx::builder::basic::kvp("field1", i),
                      bsoncxx::builder::basic::kvp("field2", i),
                      bsoncxx::builder::basic::kvp("field3", i))
                  )
              )
          };
  
          upsert_one.upsert(true); // Set upsert to true: if no document matches {"a": 1}, insert {"a": 2}.
          op0.append(upsert_one);
      }
  
      /* Fill insert bulk operations */
  
      for (int i = 0; i &lt; N_INSERTS; i++){
  
          mongocxx::model::insert_one insert_one{
              bsoncxx::builder::basic::make_document(
                  bsoncxx::builder::basic::kvp("field0", i),
                  bsoncxx::builder::basic::kvp("field1", i),
                  bsoncxx::builder::basic::kvp("field2", i)
              )
          };
  
          op1.append(insert_one);
      }
  
      /* Execute transaction */
  
      for( auto bulk_op : bulk_operations){
          try {
              bulk_op-&gt;execute();
          }
          catch (std::exception&amp; e){     
              std::cerr &lt;&lt; "Bulk write exception: " &lt;&lt; e.what() &lt;&lt; std::endl;
              session.abort_transaction();
          }
      }
  
      session.commit_transaction();   
  
      return 0;
  }
  </code></pre>
  
  <p>which you can compile using the following command in a linux system with mongocxx installed:</p>
  
  <pre><code>c++ --std=c++11 test.cpp -o test -I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/bsoncxx/v_noabi -L/usr/local/lib -lmongocxx -lbsoncxx
  </code></pre>
  
  <p>While executing I get the following error:</p>
  
  <pre><code>Bulk write exception: Exec error resulting in state DEAD :: caused by :: operation was interrupted: generic server error
  terminate called after throwing an instance of 'mongocxx::v_noabi::operation_exception'
    what():  No transaction started: generic server error
  Aborted (core dumped)
  </code></pre>
  
  <p>Also, I got different errors changing the parameters N_INSERTS and N_UPDATES:</p>
  
  <p><a href="https://docs.google.com/spreadsheets/d/1xZZs5Vb8FCXpvjL2o2fEyujWrDzCTAWCvF49IerZqHw/edit?usp=sharing" rel="nofollow noreferrer">https://docs.google.com/spreadsheets/d/1xZZs5Vb8FCXpvjL2o2fEyujWrDzCTAWCvF49IerZqHw/edit?usp=sharing</a></p>
  
  <p>Thanks!</p>