Skip to content

Instantly share code, notes, and snippets.

@rorcraft
Created January 19, 2012 02:23
Show Gist options
  • Save rorcraft/1637279 to your computer and use it in GitHub Desktop.
Save rorcraft/1637279 to your computer and use it in GitHub Desktop.
ActiveRecord changes attributes in a rolled back transaction
class User < ActiveRecord::Base
after_create :deadlock_on_first_try
def deadlock_on_first_try
raise ActiveRecord::StatementInvalid.new("Lock wait timeout exceeded") if @deadlocked.nil?
ensure
@deadlocked = true
end
end
# In the console...
>@user = User.new
>@user.name = "foo"
>User.transaction do @user.save end
ActiveRecord::StatementInvalid: Lock wait timeout exceeded
>@user.new_record?
false
>@user.id
1
>User.find(1)
ActiveRecord::RecordNotFound: Couldn't find User with id=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment