Rails+PostgreSQL:search_path 取决于子域
在我们的 Rails 2.x 应用程序中,数据库连接的 search_path
取决于连接应用程序的子域(基本上 search_path = "product_"+subdomain
)。因为 search_path
是按连接定义的,并且数据库连接在请求上共享,甚至是并发的,所以这是一个问题。出于显而易见的原因,我宁愿不将并发性更改为一次仅服务一个请求。
那么有没有一种方法可以将连接池中的数据库连接进行分组,并设置某种策略,只使用合适的连接来进行请求呢?或者有没有一种方法可以为每个子域使用一个连接池(超时后池会自动丢弃)?为每个子域启动一个 Rails 实例是不可能的,因为可能有许多空闲子域(这是某种专业帐户,您可以在其中获得子域和您自己的“世界”,与某些表中的站点的其余部分不同)。
这个问题的最佳解决方案是什么?
In our rails 2.x application the search_path
of the database connection depends on the subdomain through which the application is contacted (basically search_path = "production_"+subdomain
). Because the search_path
is defined per connection and database connections are shared over requests, even concurrently, this is a problem. I would rather not change concurrency to only serve one request at a time for obvious reasons.
So is there a way to group the database connections in the connection pool and set some kind of policy that only a fitting connection is used for the request? Or is there a way to use one connection pool per subdomain (where the pools are automatically discarded after a timeout)? Starting a rails instance for each subdomain is no option because there might be many idling subdomains (it's some kind of pro-account where you get a subdomain and your own "world" that differs from the rest of the site in some tables).
What would be the best solution for this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需在请求开始时、加载任何对象之前设置connection.search_path,就可以了。在我们的例子中,我们有一个 Rack 应用程序来包装我们的 Rails 应用程序,并根据传入域为我们进行此设置。
You can just set connection.search_path at the beginning of the request, before any objects are loaded, and you'll be fine. In our case we have a Rack app that wraps our rails app and does this setup for us based on the incoming domain.