Skip to content

Instantly share code, notes, and snippets.

@ukazap
Forked from ryanermita/rails_locking.md
Created September 27, 2018 00:44
Show Gist options
  • Save ukazap/7eb207251ebb6a92c1b67a766b0a36d1 to your computer and use it in GitHub Desktop.
Save ukazap/7eb207251ebb6a92c1b67a766b0a36d1 to your computer and use it in GitHub Desktop.

Revisions

  1. @ryanermita ryanermita revised this gist Aug 27, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_locking.md
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ or you can also use with_lock method to select and lock the record.
    ``` ruby
    user = User.find(1)
    user.with_lock do #lock the record
    # you can run other code herer.
    # you can run other code here.
    user.name = 'ryan'
    user.save!
    end
  2. @ryanermita ryanermita revised this gist Aug 27, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_locking.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    **Optimistic Locking** assumes that a database transaction conflict is very rare to happen. It uses a version number of the record to track the changes. It raise an error when other user tries to update the record while it is lock.

    usage:
    *usage*

    Just add a lock_version column to the table you want to place the lock and Rails will automatically check this column before updating the record.

  3. @ryanermita ryanermita revised this gist Aug 27, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions rails_locking.md
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,7 @@ or you can also use with_lock method to select and lock the record.
    ``` ruby
    user = User.find(1)
    user.with_lock do #lock the record
    # you can run other code herer.
    user.name = 'ryan'
    user.save!
    end
  4. @ryanermita ryanermita revised this gist Aug 27, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_locking.md
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ or you can also use with_lock method to select and lock the record.
    user = User.find(1)
    user.with_lock do #lock the record
    user.name = 'ryan'
    user.save
    user.save!
    end
    ```

  5. @ryanermita ryanermita revised this gist Aug 27, 2016. 1 changed file with 12 additions and 15 deletions.
    27 changes: 12 additions & 15 deletions rails_locking.md
    Original file line number Diff line number Diff line change
    @@ -7,24 +7,21 @@ Just add a lock_version column to the table you want to place the lock and Rails
    **Pessimistic locking** assumes that database transaction conflict is very likely to happen. It locks the record until the transaction is done. If the record is currently lock and the other user make a transaction, that second transaction will wait until the lock in first transaction is release.

    *usage*

    `user = User.lock.find(1) #lock the record`

    `user.name = 'ryan'`

    `user.save! #release the lock`
    ``` ruby
    user = User.lock.find(1) #lock the record
    user.name = 'ryan'
    user.save! #release the lock
    ```

    or you can also use with_lock method to select and lock the record.

    `user = User.find(1)`

    `user.with_lock do #lock the record`

    ` user.name = 'ryan'`

    ` user.save`

    `end`
    ``` ruby
    user = User.find(1)
    user.with_lock do #lock the record
    user.name = 'ryan'
    user.save
    end
    ```

    ### Reference
    * https://www.ibm.com/support/knowledgecenter/SSPK3V_7.0.0/com.ibm.swg.im.soliddb.sql.doc/doc/pessimistic.vs.optimistic.concurrency.control.html
  6. @ryanermita ryanermita revised this gist Aug 27, 2016. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions rails_locking.md
    Original file line number Diff line number Diff line change
    @@ -9,15 +9,21 @@ Just add a lock_version column to the table you want to place the lock and Rails
    *usage*

    `user = User.lock.find(1) #lock the record`

    `user.name = 'ryan'`

    `user.save! #release the lock`

    or you can also use with_lock method to select and lock the record.

    `user = User.find(1)`

    `user.with_lock do #lock the record`

    ` user.name = 'ryan'`

    ` user.save`

    `end`

    ### Reference
  7. @ryanermita ryanermita revised this gist Aug 27, 2016. 2 changed files with 27 additions and 11 deletions.
    11 changes: 0 additions & 11 deletions locking.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +0,0 @@
    Optimistic Locking assumes that a database transaction conflict is very rare to happen.

    Pessimistic locking assumes that database transaction conflict is very likely to happen.


    usage:

    ### Reference
    * http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html
    * http://api.rubyonrails.org/classes/ActiveRecord/Locking/Pessimistic.html
    * http://thelazylog.com/understanding-locking-in-rails-activerecord/
    27 changes: 27 additions & 0 deletions rails_locking.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    **Optimistic Locking** assumes that a database transaction conflict is very rare to happen. It uses a version number of the record to track the changes. It raise an error when other user tries to update the record while it is lock.

    usage:

    Just add a lock_version column to the table you want to place the lock and Rails will automatically check this column before updating the record.

    **Pessimistic locking** assumes that database transaction conflict is very likely to happen. It locks the record until the transaction is done. If the record is currently lock and the other user make a transaction, that second transaction will wait until the lock in first transaction is release.

    *usage*

    `user = User.lock.find(1) #lock the record`
    `user.name = 'ryan'`
    `user.save! #release the lock`

    or you can also use with_lock method to select and lock the record.

    `user = User.find(1)`
    `user.with_lock do #lock the record`
    ` user.name = 'ryan'`
    ` user.save`
    `end`

    ### Reference
    * https://www.ibm.com/support/knowledgecenter/SSPK3V_7.0.0/com.ibm.swg.im.soliddb.sql.doc/doc/pessimistic.vs.optimistic.concurrency.control.html
    * http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html
    * http://api.rubyonrails.org/classes/ActiveRecord/Locking/Pessimistic.html
    * http://thelazylog.com/understanding-locking-in-rails-activerecord/
  8. @ryanermita ryanermita renamed this gist Aug 27, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. @ryanermita ryanermita created this gist Aug 27, 2016.
    11 changes: 11 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    Optimistic Locking assumes that a database transaction conflict is very rare to happen.

    Pessimistic locking assumes that database transaction conflict is very likely to happen.


    usage:

    ### Reference
    * http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html
    * http://api.rubyonrails.org/classes/ActiveRecord/Locking/Pessimistic.html
    * http://thelazylog.com/understanding-locking-in-rails-activerecord/