Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4344748
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:56:27+00:00 2026-05-21T11:56:27+00:00

Bit of an open-ended question here, so I’ll outline the problem first. We have

  • 0

Bit of an open-ended question here, so I’ll outline the problem first. We have a Resque worker who is supposed to pop data-synchronisation jobs off a queue, the reasons are two-fold, using Cron (and paying Rails environment boot time over-and-over) sucks, and the alternative queues, well Github made a pretty good case against them when they announced Resque. Also, the Redis time-series functionality already plays a big part of our infrastructure, where we also shuffle TS data into RRDTool & etc…

Here’s the problem, with a typical three-hours between jobs (but staff can schedule jobs at any time… hence the queue), the PostgreSQL server goes away. Easy enough to cure, I expected that setting reconnect: true under the corresponding environment would ensure that this worked as expected.. I read in a few places that reconnect: true won’t work for applications that use fork(). Which, naturally Resque does, to start it’s workers… the part I don’t understand is why ActiveRecord’s reconnect can’t work under these circumstances?

I noticed the implementations of reconnect! the MySQL Adapter and the PostgreSQL Adapter in ActiveRecord are different… but either way I would expect the ActiveRecord reconnect: true configuration to work.

The problem seems to be clear enough, when the child process exists, it closes the file handles created by the parent (thus hanging up the connection to the database) – is it possible to close a filehandle in such a way that ActiveRecord doesn’t recognise that the connection has been terminated?

There’s also, for what it’s worth, an ActiveRecord aware fork() which I was able to find on Github, as a pastie – it’s untested, but I assume it works (haven’t tried it with current Rails..)

My question is more, Why can’t the automatic reconnect in AR work if you fork()? (and, latterly – I can’t be the only person who is having this problem; I’m chalking it up to using PGSQL with Resque!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:56:28+00:00Added an answer on May 21, 2026 at 11:56 am

    While not exactly the answer to “why can’t it reconnect”, but I think that what can help you is the following code put somewhere in the initialization phase:

    
    Resque.after_fork do |job|
      ActiveRecord::Base.connection.reconnect!
    end
    

    Update:
    Regarding the reconnect – it seems that it’s MySQL only feature.
    This is how it’s used in mysql adapter:
    https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb#L848
    As you can see, it uses the underlying driver’s reconnect feature.
    https://github.com/kwatch/mysql-ruby/blob/master/ext/mysql.c#L923

    On the other hand, the postgresql adapter doesn’t do anything with regard to reconnect option, you can see the https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

    Also, the PostgreSQL site explicitly says that “Forking a process with open libpq connection can lead to unpredictable results” – http://www.postgresql.org/docs/9.0/interactive/libpq-connect.html
    And it’s also evident that the C driver doesn’t provide any reconnect feature.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is a bit open ended as I'm not sure it's possible or
Sorry if this question is a bit open ended, but I'm pretty new to
somewhat open ended question here as I am mostly looking for opinions. I am
I know this is a probable open ended question, and I have tried looking
sorry if this question seems a bit open ended, but I'm often wanting to
I have a 64 bit Enterprice SuSE 11 I have an application which open
Bit of an unusual question, but I have setup a field inside a MySQL
Bit of a strange question here i know. but i wanted to know if
I have some forms that takes a bit of time to open because they
I have a bit of code that will open a Word 2007 (docx) document

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.