5

I'm developing on OSX and deploying on Linux. My environments are:

Development:

OSX Lion
Ruby 1.9.2p180
ActiveRecord 3.0.9
PostgreSQL 9.0

Test:

Ubuntu Server 11.04
Ruby 1.9.2p290
ActiveRecord 3.1.1
PostgreSQL 9.1

The following pieces of code work on OSX but not on Linux:

ei_doc_type = TaxDoc::Model::DocType.find_by_doc_type("EI")
doc_version = ei_doc_type.doc_versions.find_by_doc_version(edoc.version)
customer.electronic_invoices.create(....)

and:

ei_doc_type = TaxDoc::Model::DocType.find_by_doc_type("EI")
ei_doc_type.doc_versions.each { |doc_version|
  @mappings[doc_version.doc_version] = Hash.new
  doc_version.mappings.each { |mapping|
    @mappings[doc_version.doc_version][mapping.column_name] = mapping.xml_element
  }
}

When I try to run any of these pieces of code in Linux I get the following error:

.../connection_pool.rb:409: in `retrieve_connection': ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)

I changed the code, and it works on Linux:

ei_doc_type = TaxDoc::Model::DocType.find_by_doc_type("EI")
doc_versions = TaxDoc::Model::DocVersion.where(:doc_type_id => ei_doc_type.id, :doc_version => edoc.version)
doc_version = doc_version.first
customer.electronic_invoices.create(....)

and:

ei_doc_type = TaxDoc::Model::DocType.includes(:doc_versions => :mappings).find_by_doc_type("EI")
ei_doc_type.doc_versions.each { |doc_version|
  @mappings[doc_version.doc_version] = Hash.new
  doc_version.mappings.each { |mapping|
    @mappings[doc_version.doc_version][mapping.column_name] = mapping.xml_element
  }
}

It seems like eager loading for first order associations is not working on the test environment, and in the first example. Even if I use "includes" it does not work.

Is there a difference between versions on ActiveRecord or PostgreSQL that could be causing this?

These are my models:

class DocType < EDocsDatabase
  has_many :doc_versions, :dependent => :destroy
  has_many :mappings, :through => :doc_versions
  has_many :service_types
end

class DocVersion < EDocsDatabase
  belongs_to :doc_type
  has_many :mappings, :dependent => :destroy
end

class Mapping < EDocsDatabase
  belongs_to :doc_version
end
Chris Travers
  • 25,424
  • 6
  • 65
  • 182
Ecil
  • 1,009
  • 1
  • 13
  • 28
  • to make sure its not your version, swap your database connection and have your DEV version talk to your TEST database. You need to reconfigure (and make sure ports are open!), but atleast you know if that could cause it. You may also do it vice verse (make your TEST connect to your DEV). – Roger Aug 09 '12 at 17:37
  • 1
    you say 'development' and 'test' in your setups, do you really mean dev and production? The two environments you list have different versions of AR. It seems unlikely to me that this is a linux vs OSX issue, and more likely that you are using different versions of ActiveRecord in your two environments, and running into a bug or weird edge case in one of them. The first step is using the same version of ActiveRecord in both (which should not be hard to do, are you not using a Gemfile.lock same between them? You should be). Once you've ensured that, come back again. – jrochkind Aug 21 '12 at 23:14
  • @jrochkind it's really dev and test. but you pointed me to something i haven't noticed: AR, Ruby and PostgreSQL have different versions. i'll check that and try to fix the versions. thanks for pointing me that. – Ecil Aug 29 '12 at 00:09
  • 1
    i think it's most likely that different versions of AR matter than it is that different versions of ruby or postgres do. Since standardizing a version of AR is also easier than changing your ruby or postgres install, I'd start with that. – jrochkind Aug 30 '12 at 04:45
  • 1
    It seems like you're not using Rails -- are you sure [`ActiveRecord::Base.establish_connection`](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-establish_connection) in getting called in the test environment? – willglynn Sep 21 '12 at 22:00
  • I'm not using Rails, but I call establish_connection. – Ecil Sep 24 '12 at 12:43

1 Answers1

0

What context are you using this code in?

The reason is - currently ActiveRecord requires some connection pool management if you are running your code in a response loop (like in a server). Rails addresses this using a middleware that manages the AR connections for you outside of your main application responder. However, if you don't have this kind of a wrapper you may have other application routines "stepping" on your connection pool, especially when using threaded servers.

Julik
  • 7,676
  • 2
  • 34
  • 48
  • This issue happened around 2 years ago and it was closed on my part. Thanks for the concern. – Ecil Sep 09 '13 at 01:13