views:

61

answers:

1

Hi All, I have a rails app and a separate druby process. This process gives me some methods, and at the first line of each druby's method there is a call to ActiveRecord::Base.establish_connection, where the db_name depends on a param set by the rails application. Sometimes the process is getting the wrong database name and I think it could be a concurrency problem. Could it be? Any idea on how to make it thread-safe?

Thanks for any help! Roberto

A: 

Yes, this is a concurrency problem.

To fix it, you would have to change your architecture a little bit, but I don't have enough informations.

  • Is the code running in the backend the same as the one running in your rails application ?
  • Do your different databases use the same model ?
  • How many different databases you have ? Does this number grow ?

Basically, if you have a small and fixed number of databases, the simplest is to use different ruby processes.

If you have different databases with different models, you could envisions to use different base classes:

AppA < ActiveRecord::Base
Model1 < AppA
Model2 < AppA

AppB < ActiveRecord::Base
Model3 < AppB

Then you can call

AppA.establish_connection(...)
AppB.establish_connection(...)

to different databases.

zimbatm
You could also add a global Mutex to serialize your drb requests, but then you limit the speed.
zimbatm