I have two neo4j databases running on two different hosts. I connected my rails app to one of them while generating the app. Now I want to use other database as well with the app. How can I configure the app to connect to both the databases?
2 Answers
There’s not currently a good way to configure one Ruby process to use two sessions at the same time. If you are using Rails you can change the server by setting the NEO4J_URL environment variable. 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)
See: https://github.com/neo4jrb/neo4j/blob/master/lib/neo4j/active_base.rb

- 10,746
- 1
- 22
- 34
As Brian mentioned currently we cannot configure one Ruby process to use two sessions at the same time. 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)
The neo4j.yml
sets the Neo4j::ActiveBase.current_session
for you in the railtie. If you set Neo4j::ActiveBase.current_session
after the app has started up it will override what was in the neo4j.yml
. The current_session
needs to be a Neo4j::Core::CypherSession
object from the neo4j-core gem. (See the readme: https://github.com/neo4jrb/neo4j-core)
Also keep in mind, that currently neo4j does not support having different session for each model. 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
. To this you have to specify wrap_level: :proc
while declaring the adaptor. (Refer: 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'

- 1,303
- 13
- 25