2

我有一个 Rails 应用程序和一个单独的 druby 进程。这个过程给了我一些方法,在每个 druby 方法的第一行都有一个对 ActiveRecord::Base.establish_connection 的调用,其中 db_name 取决于 rails 应用程序设置的参数。有时该进程获取错误的数据库名称,我认为这可能是一个并发问题。可以吗?关于如何使其线程安全的任何想法?

谢谢你的帮助!罗伯托

4

1 回答 1

0

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.

于 2009-05-15T10:46:49.493 回答