ActiveRecord connection does not reconnect when using Thin/Sinatra but works fine with just Sinatra.
Reported by hbeaver (at gmail) | May 28th, 2009 @ 03:37 PM | in Future
I have a very simple model for a simple web service. Any call to model will throw 500, like Model.find(:first) after 10 seconds.
Error:
application error
ActiveRecord::StatementInvalid at /leads
Mysql::Error: MySQL server has gone away: SELECT * FROM
leads
Setting this in model does not help:
before do
ActiveRecord::Base.connection.verify!
end
I have set MySQL to 10 seconds timeout to duplicate the issue quickly using this:
SET GLOBAL wait_timeout=10;
SET GLOBAL interactive_timeout=10;
SET SESSION wait_timeout=10;
SET SESSION interactive_timeout=10;
show GLOBAL VARIABLES like 'wait_timeout';
show GLOBAL VARIABLES like 'interactive_timeout';
show SESSION VARIABLES like 'wait_timeout';
show SESSION VARIABLES like 'wait_timeout';
If I use rackup on the Sinatra app, no problem. When I use thin I receive the error. Here is my thin config:
---
pid: tmp/pids/thin.pid
log: log/thin.log
timeout: 30
port: 4600
max_conns: 1024
chdir: /Users/hbeaver/code/primedia/service_leads/
rackup: /Users/hbeaver/code/primedia/service_leads/config.ru
max_persistent_conns: 512
environment: development
daemonize: true
address: 0.0.0.0
require: []
servers: 1
Thanks,
Hank
Comments and changes to this ticket
-
hbeaver (at gmail) May 29th, 2009 @ 06:18 PM
RESOLUTION?
I moved the "before do" into extended Sinatra::Default class as opposed to model and issue was resolved. However, this does not explain why mongrel will create a new connection auto-magically whereas thin will not. Please see 2 attached tcpdumps named appropriately, 'thin_no_reconnect_example.dump' and 'mongrel_reconnect_example.dump'.
In both, the server has just been started and then a HTTP request is made to a one line action that does Model.find(:first). In both, a mysql connection is created ok after server is started. After approx. 10 seconds (because of the wait_timeout setting in the ticket I mentioned yesterday) mysql will sent a FIN/ACK (disconnect). Shortly thereafter another HTTP request is made to server. Mongrel creates yet another connection, but thin will not. I am not certain if this is a bug or just the nature of thin.
I thought it important to present this information.
Thanks again,
Hank -
hbeaver (at gmail) August 10th, 2009 @ 01:05 PM
- Assigned user cleared.
As mentioned above, I resolved this issue by moving ActiveRecord::Base.connection.verify! into Sinatra class before do block:
class NewApp < Sinatra::Default before do ActiveRecord::Base.connection.verify! end ... end #end of Sinatra class
-
Conny Brunnkvist July 21st, 2010 @ 07:15 PM
- Milestone order changed from 0 to 0
Same problem here.
Works when running in foreground but as soon as either of "-s 1" or "servers: 1" is set, ActiveRecord is can't connect.
-
Conny Brunnkvist July 26th, 2010 @ 12:22 PM
I wonder if we don't need to simply ActiveRecord::Base.establish_connection after forking.. but then, why wouldn't reconnect! work? I gave up on this one, will try again later perhaps.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป