简体   繁体   English

在单个Rails应用程序中使用两个neo4j数据库

[英]Use two neo4j databases in single rails app

I have two neo4j databases running on two different hosts. 我有两个在两个不同主机上运行的neo4j数据库。 I connected my rails app to one of them while generating the app. 在生成应用程序时,我将Rails应用程序连接到其中之一。 Now I want to use other database as well with the app. 现在,我想在应用程序中同时使用其他数据库。 How can I configure the app to connect to both the databases? 如何配置应用程序以连接到两个数据库?

There's not currently a good way to configure one Ruby process to use two sessions at the same time. 当前没有一种好的方法来配置一个Ruby进程以同时使用两个会话。 If you are using Rails you can change the server by setting the NEO4J_URL environment variable. 如果使用的是Rails,则可以通过设置NEO4J_URL环境变量来更改服务器。 Otherwise you'd need to manage the session by setting Neo4j::ActiveBase.current_session or Neo4j::ActiveBase.on_establish_session (which will set the session for each new thread, which may be needed if you are running a multi-threaded process) 否则,您需要通过设置Neo4j :: ActiveBase.current_session或Neo4j :: ActiveBase.on_ Establishment_session来管理会话(这将为每个新线程设置会话,如果您正在运行多线程进程,则可能需要)

See: https://github.com/neo4jrb/neo4j/blob/master/lib/neo4j/active_base.rb 参见: https : //github.com/neo4jrb/neo4j/blob/master/lib/neo4j/active_base.rb

As Brian mentioned currently we cannot configure one Ruby process to use two sessions at the same time. 正如Brian目前所提到的,我们不能将一个Ruby进程配置为同时使用两个会话。 We have to manage the session by setting Neo4j::ActiveBase.current_session (See: https://github.com/neo4jrb/neo4j/blob/master/lib/neo4j/active_base.rb ) 我们必须通过设置Neo4j::ActiveBase.current_session来管理会话(请参阅: https : //github.com/neo4jrb/neo4j/blob/master/lib/neo4j/active_base.rb

The neo4j.yml sets the Neo4j::ActiveBase.current_session for you in the railtie. neo4j.yml会在Railtie中为您设置Neo4j::ActiveBase.current_session If you set Neo4j::ActiveBase.current_session after the app has started up it will override what was in the neo4j.yml . 如果在应用程序启动后设置Neo4j::ActiveBase.current_session ,它将覆盖neo4j.yml中的neo4j.yml The current_session needs to be a Neo4j::Core::CypherSession object from the neo4j-core gem. current_session必须是neo4j-core gem中的Neo4j::Core::CypherSession对象。 (See the readme: https://github.com/neo4jrb/neo4j-core ) (请参阅自述文件: https : //github.com/neo4jrb/neo4j-core

Also keep in mind, that currently neo4j does not support having different session for each model. 还请记住,当前neo4j不支持为每个模型使用不同的会话。 So you might experience problem if, setting the session inside model. 因此,如果在模型内设置会话,则可能会遇到问题。 A better way would be to set session in the normal runtime of the app. 更好的方法是在应用的正常运行时设置会话。 You also might want to wrap the Neo4j::Core::CypherSession to get Query Proxy instead of Neo4j::Core objects . 您可能还需要包装Neo4j::Core::CypherSession以获得查询代理而不是Neo4j::Core objects To this you have to specify wrap_level: :proc while declaring the adaptor. 为此,您必须在声明适配器时指定wrap_level: :proc (Refer: https://github.com/neo4jrb/neo4j/blob/master/lib/neo4j/session_manager.rb#L14 ) (请参阅: https : //github.com/neo4jrb/neo4j/blob/master/lib/neo4j/session_manager.rb#L14

So in all, here is what you need to do 所以总而言之,这是您需要做的

http_adaptor = Neo4j::Core::CypherSession::Adaptors::HTTP.new('http://neo4j:7474',{wrap_level: :proc}) Neo4j::ActiveBase.current_session = Neo4j::Core::CypherSession.new(http_adaptor)

this will establish a wrapped session with the desired database in 'http://neo4j:7474' 这将与'http://neo4j:7474'的所需数据库建立一个包装的会话

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM