2

I am building a neo4j ogm service using ogm in java. I need to connect to 2 neo4j server from my service to handle failover and replication. Is it possible to create multiple session each towards different neo4j server from one ogm service.

Swapnil
  • 121
  • 1
  • 4
  • If you want to create two sessions to two different databases to keep them in sync I guess you can. However, you'll never - since it will be 2 or more different sessions - be able to guarantee success or failure for both without setting up a very complex system (it's called a cluster and it is not an easy problem). Seriously ... if those are your requirements and you are running this in production ... consider an enterprise edition cluster. You'll save yourself a lot of pain. – Tom Geudens Aug 11 '17 at 19:07
  • Thanks @TomGeudens .. Yes its a complex problem to solve . Enterprise would really be a way to go .. But before going forward we want to be sure about the value add viz-a-viz investment. We are banking on existing infrastructure to solve our HA problems till then. Can you provide some pointers on how to create multiple sessions inside OGM service? – Swapnil Aug 11 '17 at 19:25
  • Well, I'm not an OGM user, but looking at the documentation (https://neo4j.com/docs/ogm-manual/current/reference/) it would seem that you can manually (instead of using a properties file) set up Configuration objects (and that would allow you to have multiple) and pass those explicitely to a SessionFactory (again allowing you to have multiple of those). You'd then be able to have two sessions doing the same thing ... to different databases. Haven't tried it myself, but it's worth the experiment. – Tom Geudens Aug 14 '17 at 12:44

1 Answers1

1

You can, in theory, create multiple SessionFactory instances, pointing to different database instances and perform each operation on both. Just use Java configuration instead of property file (this is true for OGM only, SDN would not be that simple).

There are multiple things to look out for:

  • you can't rely on the the auto generated ids as they could be different on each database instance

  • when writing to 2 instances, write to first instances may (for various reasons - network issues, latencies, concurrency etc..) succeed and write to second may fail, or vice versa - your code would need to handle that somehow

  • concurrency in general - queries dependent on state of the database may behave differently on the two instances because of one of them received more updates than the other (the second is "behind")

Because of all these reasons I wouldn't recommend such solution at all.

You would be better off with either Neo4j's HA or causal cluster. See the website regarding licensing.

František Hartman
  • 14,436
  • 2
  • 40
  • 60
  • 1
    Thanks .. I tried creating multiple sessionfactory but it did not worked. I am using version 2.1.3. Code on github suggest that there is only 1 instance of driver and configuration in Components and will be overwritten by subsequent sessionfactory. Correct me if I am wrong. Looks like 3.x.x version would change that . – Swapnil Aug 22 '17 at 13:06