Code Monkey home page Code Monkey logo

Comments (5)

mceachen avatar mceachen commented on June 17, 2024

(original author here, but realize it's been 6 years since I wrote this, and I'm no longer the maintainer).

If you're not in a transaction, (depending on the rdbms, but certainly true with MySQL), consistency guarantees are simply not present. It's never a bad idea to be in a transaction, as autocommit (at least used to be) much slower than explicit transaction boundaries.

from with_advisory_lock.

muxcmux avatar muxcmux commented on June 17, 2024

Thanks for jumping on this one (SO op here). Can you elaborate a bit on “consistency guarantees are not present”? Does that mean that locks work only some of the time, or would it mean that in the context of Rails, acquiring db lock is not always guaranteed?

@allanohorn Thanks for the tip on ‘create_or_find_by’ - I ended up implementing something very similar that relies on catching unique constraint exceptions before I knew about ’create_or_find_by’

from with_advisory_lock.

mceachen avatar mceachen commented on June 17, 2024

With MySQL, if you're not in a txn, read results may not reflect the committed state of the database, depending on what engine you're using and how you've configured it.

from with_advisory_lock.

ismailakbudak avatar ismailakbudak commented on June 17, 2024

I have same problem with rspec tests. When I disable with_advisory_lock, it works as expected

from with_advisory_lock.

caifara avatar caifara commented on June 17, 2024

I had some problems but I've been able to resolve them by setting rspecs use_transactional_fixtures to false (as I should have done in the first place given I use database_cleaner).

I've written two tests to be able to see that w/o locks two threads run async and with locks they get synchronized. They work as expected, both letting database_cleaner use transactions or not.

RSpec.describe :with_advisory_lock, type: :model do
  def thread_1(lock, result)
    Thread.new do
      ActiveRecord::Base.with_advisory_lock(lock) do
        result << "thread_1"
        sleep 0.01
        result << "/thread_1"
      end
    end
  end

  def thread_2(lock, result)
    Thread.new do
      ActiveRecord::Base.with_advisory_lock(lock) do
        result << "thread_2"
      end
    end
  end

  it "can run two different synchronized tasks in parallel" do
    global_result = []

    10.times do
      result = []
      [
        thread_1("lock_1", result),
        thread_2("lock_2", result)
      ].each(&:join)

      global_result << result
    end

    expect(global_result).to include(%w[thread_1 thread_2 /thread_1])
  end

  it "can synchronize tasks with the same key" do
    10.times do
      result = []
      [
        thread_1("same_lock", result),
        thread_2("same_lock", result)
      ].each(&:join)

      # check thread_1 and /thread_1 are siblings
      expect(result.index("thread_1")).to eq(result.index("/thread_1") - 1)
    end
  end
end

from with_advisory_lock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.