# AR Stale object on SQL Server w/ lock\_version

**URL:** <https://www.ruby-forum.com/t/ar-stale-object-on-sql-server-w-lock-version/79202>\
**Category:** Rails\
**Created:** [November 29, 2005, 4:09pm UTC](https://www.ruby-forum.com/t/ar-stale-object-on-sql-server-w-lock-version/79202 "2005-11-29T16:09:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter.fitzgibbons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@peter.fitzgibbons](https://www.ruby-forum.com/u/peter.fitzgibbons)\
**Post date:** [November 29, 2005, 4:09pm UTC](https://www.ruby-forum.com/t/ar-stale-object-on-sql-server-w-lock-version/79202/1 "2005-11-29T16:09:25Z")

</div>

Hello all,

I’m getting stale-object-error from AR on objects that have lock\_version  
defined in MS SQL Server 2000.  
The problem is not repeatable as of yet, and is happening on something  
as  
simple as the User table from LoginGenerator, to which I’ve added:

lock\_version int default(0) not null

Has anyone experienced this problem?

Here’s some script/console output (this is actually from James A.'s  
LoginEngine). Notice that nothing is different between the User.find  
calls.

> > user = User.find(:first, :conditions=\>“login = ‘admin’”)  
> > =\> #\<User:0x372ebb0  
> > @attributes={“salt”=\>“ad71f490168ce50858feb1ebec0ec208b316d130”,  
> > “delete\_after”=\>nil, “updated\_at”=\>  
> > “2005/11/28 12:40:32”, “lock\_version”=\>2, “security\_token”=\>nil,  
> > “role”=\>nil, “lastname”=\>nil, “firstname”=\>nil, “id”=\>4  
> > 8, “deleted”=\>0, “token\_expiry”=\>nil, “verified”=\>1,  
> > “password\_orig”=\>“tmp”,  
> > “logged\_in\_at”=\>nil, “salted\_password”=\>“fd  
> > 57d7ba5818df53eb6fd2c53501d856c272a534”, “login”=\>“admin”,  
> > “created\_at”=\>“2004/09/01 15:53:34”, “email”=\>nil}\>
> 
> > user.change\_password(‘testing’, ‘testing’)  
> > =\> true
> 
> > user.save  
> > ActiveRecord::StaleObjectError: Attempted to update a stale object  
> > from  
> > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active\_record/locking.rb:42:in  
> > `update_without_callb acks' from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/callbacks.rb:274:in`update\_without\_ti  
> > mestamps’  
> > from  
> > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active\_record/timestamp.rb:39:in  
> > `update' from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:1431:in`create\_or\_update\_with  
> > out\_callbacks’  
> > from  
> > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active\_record/callbacks.rb:249:in  
> > `create\_or\_update’

```
    from 

```

C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active\_record/base.rb:1231:in  
`save_without_validati on' from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/validations.rb:687:in`save\_without\_tr  
ansactions’  
from  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active\_record/transactions.rb:126:in  
`save' from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/transactions.rb:126:in`transaction’  
from  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active\_record/transactions.rb:91:in  
`transaction' from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/transactions.rb:118:in`transaction’  
from  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active\_record/transactions.rb:126:in  
`save’  
from (irb):4  
from :0

> > user = User.find(:first, :conditions=\>“login = ‘admin’”)  
> > =\> #\<User:0x3a6ad48  
> > @attributes={“salt”=\>“ad71f490168ce50858feb1ebec0ec208b316d130”,  
> > “delete\_after”=\>nil, “updated\_at”=\>  
> > “2005/11/28 12:40:32”, “lock\_version”=\>2, “security\_token”=\>nil,  
> > “role”=\>nil, “lastname”=\>nil, “firstname”=\>nil, “id”=\>4  
> > 8, “deleted”=\>0, “token\_expiry”=\>nil, “verified”=\>1,  
> > “password\_orig”=\>“tmp”,  
> > “logged\_in\_at”=\>nil, “salted\_password”=\>“fd  
> > 57d7ba5818df53eb6fd2c53501d856c272a534”, “login”=\>“admin”,  
> > “created\_at”=\>“2004/09/01 15:53:34”, “email”=\>nil}\>
> 
> > user.change\_password(‘testing’, ‘testing’)  
> > =\> true
> 
> > user.save  
> > =\> true

–

---

<div class="post-metadata">

**Author:** ![peter.fitzgibbons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@peter.fitzgibbons](https://www.ruby-forum.com/u/peter.fitzgibbons)\
**Post date:** [November 29, 2005, 4:13pm UTC](https://www.ruby-forum.com/t/ar-stale-object-on-sql-server-w-lock-version/79202/2 "2005-11-29T16:13:29Z")

</div>

On 11/29/05, Peter F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello all,
> 
> I’m getting stale-object-error from AR on objects that have lock\_version  
> defined in MS SQL Server 2000.  
> The problem is not repeatable as of yet, and is happening on something as  
> simple as the User table from LoginGenerator, to which I’ve added:
> 
> lock\_version int default(0) not null
> 
> Has anyone experienced this problem?

I assume you’ve read up on ActiveRecord optimistic locking?

[http://rails.rubyonrails.com/classes/ActiveRecord/Locking.html](http://rails.rubyonrails.com/classes/ActiveRecord/Locking.html)

In my limited use it’s worked exactly as expected. Can you check that  
the lock\_version is in fact the same when you’re saving? Look at the  
logs…

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![peter.fitzgibbons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@peter.fitzgibbons](https://www.ruby-forum.com/u/peter.fitzgibbons)\
**Post date:** [November 29, 2005, 9:21pm UTC](https://www.ruby-forum.com/t/ar-stale-object-on-sql-server-w-lock-version/79202/3 "2005-11-29T21:21:14Z")

</div>

On 11/29/05, Rick O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > lock\_version int default(0) not null
> 
> –  
> rick  
> [http://techno-weenie.net](http://techno-weenie.net)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

The lock\_version in the logs match… just like the two gets from the  
database in teh example above match.  
Also, since I’m working in a confined space (development environment),  
there  
are no other objects or instances accessing the database or Rails at the  
time of this test.

## Further ideas ?
