# MySQL server has gone away when record.save

**URL:** <https://www.ruby-forum.com/t/mysql-server-has-gone-away-when-record-save/111496>\
**Category:** Rails\
**Created:** [August 7, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/mysql-server-has-gone-away-when-record-save/111496 "2007-08-07T23:11:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [August 7, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/mysql-server-has-gone-away-when-record-save/111496/1 "2007-08-07T23:11:09Z")

</div>

Hi, I’m encountering some strangeness on my production server, and I’m  
hoping someone has light to shed. I have a table ‘new\_titles’ with one  
record and a custom ActiveRecord connection to the database w/ this  
table mapped. I can select the record, but cannot save it. The problem  
in not reproducible on my localhost either (table and all files are  
same). Here’s what I’m getting:

```auto
irb(main):002:0> test = NewTitle.find(:first)
=> #<NewTitle:0xb76e35e0 @attributes={"title"=>"Rails API",
"url"=>"http://api.rubyonrails.org/", "id"=>"1",
"description"=>"Bookmark it please"}>
irb(main):003:0> puts test.title
Rails API
=> nil
irb(main):004:0> test.title = 'Ruby on Rails API'
=> "Ruby on Rails API"
irb(main):005:0> test.save!
ActiveRecord::StatementInvalid: Mysql::Error: MySQL server has gone
away: UPDATE new_titles SET `title` = 'Ruby on Rails API',
`description` = 'Bookmark it please', `url` = 'http://
api.rubyonrails.org/' WHERE `id` = 1
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/connection_adapters/abstract_adapter.rb:128:in `log'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/connection_adapters/mysql_adapter.rb:243:in `execute'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/connection_adapters/mysql_adapter.rb:258:in `update'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/base.rb:1796:in `update_without_lock'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/locking/optimistic.rb:60:in `update_without_callbacks'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/callbacks.rb:267:in `update_without_timestamps'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/timestamp.rb:48:in `update'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/base.rb:1789:in `create_or_update_without_callbacks'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/callbacks.rb:242:in `create_or_update'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/base.rb:1551:in `save_without_validation!'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/validations.rb:762:in `save_without_transactions!'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/transactions.rb:133:in `save!'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/connection_adapters/abstract/database_statements.rb:
59:in `transaction'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/transactions.rb:95:in `transaction'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/transactions.rb:121:in `transaction'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/transactions.rb:133:in `save!'
        from (irb):5irb(main):006:0>
```

Running the query directly on the MySQL command-line works as  
expected.

There must be a difference between my local environment and that of  
production (other than the obvious Mac/Linux difference). Gems are all  
the same version, and MySQL hasn’t changed in either location. Any  
ideas out there?

Thanks,  
Aaron

---

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [August 8, 2007, 12:49am UTC](https://www.ruby-forum.com/t/mysql-server-has-gone-away-when-record-save/111496/2 "2007-08-08T00:49:19Z")

</div>

Problem solved. Need to run

```auto
ActiveRecord::Base.connection.reconnect! unless
ActiveRecord::Base.connection.active?
```

right before query. Seems that it should reconnect automagically if  
you’re running a query, but apparently it does not.
