Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
'ActiveRecord::ConnectionNotEstablished'
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  12 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
concept47  
View profile  
 More options May 8 2012, 1:07 am
From: concept47 <ikecof...@gmail.com>
Date: Mon, 7 May 2012 22:07:49 -0700 (PDT)
Local: Tues, May 8 2012 1:07 am
Subject: 'ActiveRecord::ConnectionNotEstablished'

I know this is a repeat of the infamous problem referenced
here,
http://groups.google.com/group/rufus-ruby/browse_thread/thread/23a8fb...
and
here, http://jmettraux.wordpress.com/2008/09/14/the-scheduler-and-the-activ...

but I can't get this to work properly for the life of me.
I have several call like this

    scheduler.every '15m' do
        Url.update_xxxxx
    end

that just go out to an rss feed and populate my db with new stories.
When I run it *without* :blocking => true, I my mysql connections slowly
get used up until they hit my limit (even if I keep increasing it)

if I run it like this

    scheduler.every '15m' do
        Url.update_xxxxx
        ActiveRecord::Base.remove_connection
    end

then I get a ''ActiveRecord::ConnectionNotEstablished'" error, which
persists even if I do this

    scheduler.every '15m' do
        ActiveRecord::Base.verify_active_connections!
        Url.update_xxxxx
        ActiveRecord::Base.remove_connection
    end

The only way to get this to run properly is with :blocking set to true, but
then I have a problem with jobs stacked on top of each other that just hogs
my cpu
Any ideas?

i'm using ruby 1.8.7 with rails 2.3.11, and rufus-scheduler (2.0.10)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Mettraux  
View profile  
 More options May 8 2012, 1:13 am
From: John Mettraux <jmettr...@gmail.com>
Date: Tue, 8 May 2012 14:13:27 +0900
Local: Tues, May 8 2012 1:13 am
Subject: Re: [rufus:737] 'ActiveRecord::ConnectionNotEstablished'

Hello,

could the new :mutex attribute help?

---8<---
scheduler.every '15m', :mutex => 'ar_mutex' do
  ActiveRecord::Base.verify_active_connections!
  Url.update_xxxxx
  #ActiveRecord::Base.remove_connection
end
--->8---

Making sure that all the rufus jobs dealing with active record share the same
mutex [name]...

Could that help?

--
John Mettraux - http://lambda.io/jmettraux


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ike Ofili  
View profile  
 More options May 8 2012, 1:22 am
From: Ike Ofili <ikecof...@gmail.com>
Date: Tue, 8 May 2012 00:22:38 -0500
Local: Tues, May 8 2012 1:22 am
Subject: Re: [rufus:745] 'ActiveRecord::ConnectionNotEstablished'

hmmm, I'll try it, but I thought each thread would still open a new
Active record connection (which is what I'm trying to avoid)

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Mettraux  
View profile  
 More options May 8 2012, 1:24 am
From: John Mettraux <jmettr...@gmail.com>
Date: Tue, 8 May 2012 14:24:24 +0900
Local: Tues, May 8 2012 1:24 am
Subject: Re: [rufus:738] 'ActiveRecord::ConnectionNotEstablished'

On Tue, May 08, 2012 at 12:22:38AM -0500, Ike Ofili wrote:

> hmmm, I'll try it, but I thought each thread would still open a new
> Active record connection (which is what I'm trying to avoid)

OK, if I remember correctly, ActiveRecord places its connections in a hash
where the key is the thread's object_id...

Cheers,

--
John Mettraux - http://lambda.io/jmettraux


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ike Ofili  
View profile  
 More options May 8 2012, 1:31 am
From: Ike Ofili <ikecof...@gmail.com>
Date: Tue, 8 May 2012 00:31:32 -0500
Local: Tues, May 8 2012 1:31 am
Subject: Re: [rufus:747] 'ActiveRecord::ConnectionNotEstablished'
What would be the advantage of using mutexes in this particular case John?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Mettraux  
View profile  
 More options May 8 2012, 1:34 am
From: John Mettraux <jmettr...@gmail.com>
Date: Tue, 8 May 2012 14:34:31 +0900
Local: Tues, May 8 2012 1:34 am
Subject: Re: [rufus:740] 'ActiveRecord::ConnectionNotEstablished'

On Tue, May 08, 2012 at 12:31:32AM -0500, Ike Ofili wrote:
> What would be the advantage of using mutexes in this particular case John?

Now, thinking a bit more about, I'd say none, unless you have jobs that don't
deal with ActiveRecord (and wouldn't have to wait on the mutex).

John


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
concept47  
View profile  
 More options May 8 2012, 1:49 am
From: concept47 <ikecof...@gmail.com>
Date: Mon, 7 May 2012 22:49:22 -0700 (PDT)
Local: Tues, May 8 2012 1:49 am
Subject: Re: [rufus:740] 'ActiveRecord::ConnectionNotEstablished'

> Now, thinking a bit more about, I'd say none, unless you have jobs that
> don't
> deal with ActiveRecord (and wouldn't have to wait on the mutex).

Ah right, so there nothing we can do, except (possibly)
explicitly ActiveRecord::Base.establish_connection
and ActiveRecord::Base.remove_connection before/after the ActiveRecord
call?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Mettraux  
View profile  
 More options May 8 2012, 1:53 am
From: John Mettraux <jmettr...@gmail.com>
Date: Tue, 8 May 2012 14:53:10 +0900
Local: Tues, May 8 2012 1:53 am
Subject: Re: [rufus:743] 'ActiveRecord::ConnectionNotEstablished'

On Mon, May 07, 2012 at 10:49:22PM -0700, concept47 wrote:

> Ah right, so there nothing we can do, except (possibly)
> explicitly ActiveRecord::Base.establish_connection
> and ActiveRecord::Base.remove_connection before/after the ActiveRecord
> call?

Yes. Probably.

---8<---
def ActiveRecord.connect(&block)

  ActiveRecord::Base.establish_connection
  block.call
  ActiveRecord::Base.remove_connection
end
--->8---

Could help, maybe it already exists...

Cheers,

--
John Mettraux - http://lambda.io/jmettraux


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ike Ofili  
View profile  
 More options May 9 2012, 4:14 pm
From: Ike Ofili <ikecof...@gmail.com>
Date: Wed, 9 May 2012 15:14:04 -0500
Local: Wed, May 9 2012 4:14 pm
Subject: Re: [rufus:749] 'ActiveRecord::ConnectionNotEstablished'

> Now, thinking a bit more about, I'd say none, unless you have jobs that don't
> deal with ActiveRecord (and wouldn't have to wait on the mutex).

> John

Another question for you, do jobs create new threads each time they
run? Or do they run in the same thread all the time?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Mettraux  
View profile  
 More options May 9 2012, 4:20 pm
From: John Mettraux <jmettr...@gmail.com>
Date: Thu, 10 May 2012 05:20:41 +0900
Local: Wed, May 9 2012 4:20 pm
Subject: Re: [rufus:744] 'ActiveRecord::ConnectionNotEstablished'

On Wed, May 09, 2012 at 03:14:04PM -0500, Ike Ofili wrote:
> > Now, thinking a bit more about, I'd say none, unless you have jobs that don't
> > deal with ActiveRecord (and wouldn't have to wait on the mutex).

> Another question for you, do jobs create new threads each time they
> run? Or do they run in the same thread all the time?

Hello Ike,

yes, by default each job executes in a new, dedicated thread, unless you use
:blocking => true.

Best regards,

--
John Mettraux - http://lambda.io/jmettraux


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ike Ofili  
View profile  
 More options May 9 2012, 4:36 pm
From: Ike Ofili <ikecof...@gmail.com>
Date: Wed, 9 May 2012 15:36:59 -0500
Local: Wed, May 9 2012 4:36 pm
Subject: Re: [rufus:753] 'ActiveRecord::ConnectionNotEstablished'
Aaaaaah this explains my problem! :D
Each of my jobs starts a thread, opens up a new ActiveRecord
connection and then doesn't close it, so my connection pool gets maxed
out within a few hours.

I've tried using

ActiveRecord::Base.connection_pool.with_connection do |connection|
    do something
end

but then I have to use the connection which means I can't call a class
method on my ActiveRecord model ... so close ... but yet so far :\


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
concept47  
View profile  
 More options May 9 2012, 10:18 pm
From: concept47 <ikecof...@gmail.com>
Date: Wed, 9 May 2012 19:18:11 -0700 (PDT)
Local: Wed, May 9 2012 10:18 pm
Subject: Re: [rufus:753] 'ActiveRecord::ConnectionNotEstablished'

So here are my findings, hopefully they help someone else.

using

*ActiveRecord::Base.connection_pool.with_connection do |conn|*
*  User.find(1) *
*end*

or even better

*ActiveRecord::Base.connection_pool.with_connection{ User.first }*

should check out a connection from the pool set aside for Rails in your
database.yml pool setting, let your active record call use it and then
check it back in, solving our problem.
**But this only works in rails 3+** ... you can see the code change here

Rails 2.3
http://apidock.com/rails/v2.3.8/ActiveRecord/ConnectionAdapters/Conne...
Rails
3 http://apidock.com/rails/v3.0.0/ActiveRecord/ConnectionAdapters/Conne...

This guy (you should read all his stuff on threading and ActiveRecord if
you're trying to make this work btw) explains the patch in this blog post
http://coderrr.wordpress.com/2009/05/05/activerecords-with_connection...

AFAIK, nothing else seems to work for rails 2.3, I tried running

*ActiveRecord::Base.connection_pool.clear_stale_cached_connections! *every
5-10 minutes

tried  *ActiveRecord::Base.clear_active_connections!*

nothing worked, so its back to *:blocking => true* for me.
Thanks for all the help John!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »