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