Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveRecord::ConnectionTimeoutError on rails model callbacks #626

Closed
alenteria opened this issue Aug 11, 2015 · 7 comments
Closed

ActiveRecord::ConnectionTimeoutError on rails model callbacks #626

alenteria opened this issue Aug 11, 2015 · 7 comments

Comments

@alenteria
Copy link

I am using EM.def in some of my rails Model callbacks and sometimes it throws error
ActiveRecord::ConnectionTimeoutError (could not obtain a database connection within 5.000 seconds (waited 5.000 seconds))

@sodabrew
Copy link
Contributor

Please provide enough information for this bug report.

@alenteria
Copy link
Author

My model Photo, has an attached file used with paperclip.
Theres a callback 'generate_watermarked_preview' on after_commit
I enclosed the watermark generation logic with Event Machine.
It looks something like this

EM.run do
      non_blocking = proc do
        .. some codes here
      end
     EM.defer non_blocking
end

But if I uploaded many photos and this callback was being called many times, it throws this error
ActiveRecord::ConnectionTimeoutError (could not obtain a database connection within 5.000 seconds (waited 5.000 seconds))

@sodabrew
Copy link
Contributor

Some related discussion: https://www.ruby-forum.com/topic/195010
I think the idea is that you may be creating / checking out new connections from the shared pool faster then you're checking them back in. Which database adapter are you using, btw?

@alenteria
Copy link
Author

I am using postgresql.
Ok thanks @sodabrew. I'll read that forum.

@alenteria
Copy link
Author

I solved it by making sure the connection pool has been released.
ActiveRecord::Base.connection_pool.release_connection

EM.run do
      non_blocking = proc do
        begin
          .. some codes here
        ensure
           ActiveRecord::Base.connection_pool.release_connection
        end
     end
     EM.defer non_blocking
end

@sodabrew
Copy link
Contributor

Good to hear! Do you think there's some documentation improvements you could suggest to help this situation?

@alenteria
Copy link
Author

alenteria commented Aug 11, 2015

Yes, much better if you provide more examples in different scenarios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants