Rails app without any database connection

1,609 views
Skip to first unread message

Kira Corina

unread,
Jan 14, 2011, 6:23:17 AM1/14/11
to Phusion Passenger Discussions
Hi guys,

I have multiple legacy DBs and a Rails app.

Initially there is no any DB connection in the app.
(I turn ActiveRecord off using these instructions:
http://stackoverflow.com/questions/2212709/remove-activerecord-in-rails-3-beta)

After a user logs in, the app dynamically connects to an appropriate
DB. (ActiveRecond::Base.establish_connection...)

There is no database.yml file.

Everything works fine on Webrick, but when deploying on Nginx/
Passenger I get "ActiveRecord::ConnectionNotEstablished" error, and in
the stacktrace I see that Passenger tries to “eager load” the models
(and fails because no user is logged in and therefore no DB is
selected).

Is there a way to turn this off?

Thanks for any pointing,
Kira

Hongli Lai

unread,
Jan 14, 2011, 11:36:30 AM1/14/11
to phusion-...@googlegroups.com
Can you post the stack trace?

> --
> You received this message because you are subscribed to the Google Groups "Phusion Passenger Discussions" group.
> To post to this group, send email to phusion-...@googlegroups.com.
> To unsubscribe from this group, send email to phusion-passen...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/phusion-passenger?hl=en.
>
>

--
Phusion | Ruby & Rails deployment, scaling and tuning solutions

Web: http://www.phusion.nl/
E-mail: in...@phusion.nl
Chamber of commerce no: 08173483 (The Netherlands)

Brandon Dewitt

unread,
Jan 15, 2011, 12:51:48 PM1/15/11
to Phusion Passenger Discussions
Let me start by saying there is a lot of context in your question that
I don't fully understand, but I will do my best to point you in the
direction I think you want to go.

1. This issue is not attributable to nginx/passenger but rather the
Rails environment.

- While using Webrick you are running in "development" mode which
means Rails will use Rails.root/config/environments/development.rb on
startup to load the environment. The default development.rb file has
a line "config.cache_classes = false" which tells the application to
not cache the class definition because you are in "development" and
want to be able to change the class definitions while the server is
running.

- When you deployed to passenger the default environment is production
(it assumes you are deploying for production because nginx/passenger
is often used for production). Your production.rb file sets
"config.cache_classes" to true which then loads the models.

as you can see in this snippet from the rails source, if cache_classes
== true and it is not a rake task the classes are eager loaded (or
cached).

module Rails
class Application
module Finisher
# ...
initializer :eager_load! do
if config.cache_classes && !$rails_rake_task
ActiveSupport.run_load_hooks(:before_eager_load, self)
eager_load!
end
end
# ...
end
end
end


2. I think we are missing some context about which parts of the
solution outlined in the link you provided were followed. (I looked
at the link and the solution and if you did all of the steps [remove
active_record railtie, remove AR from Gemfile] then it wouldn't work
in any environment)

- If you remove the active_record gem from the Gemfile then you can't
call in AR to establish the connection when a user authenticate. If
you remove the railtie then you won't have access to it from within
rails.

- I think you want to leave those file included and just override the
default behavior of rails when initiating a request. (Is there any
model shared between users? if so you should override AR conn on that
model. Do you have a finite set of DB's to connect to? If so you may
want to create a connection pool-pool data structure in memory with
active connections to each so each request does not have the overhead
of establishing a new connection)

Overall you can "fix" this by setting cache_classes, but depending on
volume/performance that really isn't a long term solution. You should
think about how you want to manage this long term and move in that
direction, the possibilities are numerous as this type of database
strategy has many variables and constraints to be thought through.
The guys as Phusion can take care of the Server side (and they do
great), but can't make the architectural decision.

All the luck,

bsd

On Jan 14, 6:23 am, Kira Corina <kira.cor...@gmail.com> wrote:
> Hi guys,
>
> I have multiple legacy DBs and a Rails app.
>
> Initially there is no any DB connection in the app.
> (I turn ActiveRecord off using these instructions:http://stackoverflow.com/questions/2212709/remove-activerecord-in-rai...)

Kira Corina

unread,
Jan 17, 2011, 4:31:22 AM1/17/11
to Phusion Passenger Discussions
Hongli, Brandon,

Thank you very much. I think this was indeed because of the
"config.cache_classes" option, so I'll try unsetting this when I have
the production instance available next time. In fact, I tried running
Webrick in both "development" and "production" modes and that crossed
me up. I've just tried it again, and production didn't work on Webrick
either, so you're right.

Connection pool is a good idea, I was also thinking about this.

Again, thank you very much.
Best wishes,
Kira

Kira Corina

unread,
Jan 17, 2011, 4:31:31 AM1/17/11
to Phusion Passenger Discussions
Hongli, Brandon,

Thank you very much. I think this was indeed because of the
"config.cache_classes" option, so I'll try unsetting this when I have
the production instance available next time. In fact, I tried running
Webrick in both "development" and "production" modes and that crossed
me up. I've just tried it again, and production didn't work on Webrick
either, so you're right.

Connection pool is a good idea, I was also thinking about this.

Again, thank you very much.
Best wishes,
Kira

On 15 янв, 19:51, Brandon Dewitt <brandonsdew...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages