-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 2.9.0
-
Component/s: Docs
-
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?