Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-1870

Transactions documentation does not pass session to operations

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.10.0.rc0
    • Affects Version/s: 2.9.0
    • Component/s: Docs
    • Labels:
    • Environment:
      MongoDB4.0.10
      use replica set
    • Fully Compatible

      When using transactions, the session must be explicitly passed to each operation. The with_transaction documentation on https://docs.mongodb.com/ruby-driver/current/tutorials/ruby-driver-transactions/ does not do so.

      Changes to be made:

      1. Explicitly call out that session must be passed to each operation in transaction.
      2. Fix the insert_one calls.
      3. Add a read operation which also demonstrates passing the session.

      -------

      Original report:

      Hello

      I tried transaction as described in the ruby-driver tutorial but it did not go well.

      Specifically, the abort transaction did not work, and the insert was done.

      https://docs.mongodb.com/ruby-driver/current/tutorials/ruby-driver-transactions/

       

      Instead, I tried the following method and it worked well.

       

      lib/connection.rb

       

      require 'mongo'
      
      class Connection
        def initialize
          Mongo::Logger.logger.level = ::Logger::FATAL
          @client = Mongo::Client.new('mongodb://foo:bar@mongo1.com,mongo2.com,mongo3.com/foo?replicaSet=mongo-bar&ssl=true')
        end
      
        def client
          @client
        end
      
        def foo
          @client[:Foo]
        end
      end
      
      require 'mongo'
      require_relative './lib/connection'
      
      conn = Connection.new
      
      cli = conn.client
      session = cli.start_session read_preference: { mode: :primary }
      
      session.with_transaction(
          read_concern: {level: :snapshot},
          write_concern: {w: :majority},
          read: {mode: :primary}
      ) do |ses| 
        d = {}
      
        res_insert = conn.foo.insert_one(d, session: ses)
        p res_insert 
      
        res_find = conn.foo.find(nil, session: ses).first
        p res_find
      
        res_abort = session.abort_transaction
        p res_abort
      
      end
      
      session.end_session

       

      Could you give me a lecture on the right way?

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            junpayment Junpei Nomura
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: