Sha256: 6c94b6a25fbc2487bf9675b17003b8fe34810f41447bdcfd26e735a24b490889

Contents?: true

Size: 1.48 KB

Versions: 22

Compression:

Stored size: 1.48 KB

Contents

# Copyright 2021 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.

require "io/console"
require_relative "../config/environment"
require_relative "models/singer"
require_relative "models/album"

class Application
  def self.run
    # Get a random singer and then update the singer in a separate thread.
    # This simulates a concurrent update of the same record by two different processes.
    singer = Singer.all.sample

    puts ""
    puts "Singer #{singer.first_name} #{singer.last_name} with version #{singer.lock_version} loaded"

    t = Thread.new do
      # Load the singer in the separate thread into a separate variable.
      singer2 = Singer.find singer.id
      singer2.update last_name: "Rashford"
      puts ""
      puts "Updated the last name of the singer to #{singer2.last_name}"
    end
    t.join

    # Now try to update the singer in the main thread. This will fail, as the lock_version number has been increased
    # by the update in the separate thread.
    begin
      singer.update last_name: "Drake"
    rescue ActiveRecord::StaleObjectError
      puts ""
      puts "Updating the singer in the main thread failed with a StaleObjectError"
    end

    singer.reload
    puts "Reloaded singer data: #{singer.first_name} #{singer.last_name}, version: #{singer.lock_version}"

    puts ""
    puts "Press any key to end the application"
    STDIN.getch
  end
end

Application.run

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
activerecord-spanner-adapter-1.8.0 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.6.3 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.6.2 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.6.1 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.6.0 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.5.1 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.5.0 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.4.4 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.4.3 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.4.2 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.4.1 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.4.0 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.3.1 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.2.2 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.2.1 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.2.0 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.1.0 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.0.1 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-1.0.0 examples/snippets/optimistic-locking/application.rb
activerecord-spanner-adapter-0.7.0 examples/snippets/optimistic-locking/application.rb