Exposing embedded Neo4J OGM database via bolt

352 Views Asked by At

I am using Neo4J embedded database with OGM and creating the database service in a directory via the OGM SessionFactory:

Configuration configuration = new Configuration.Builder()
                .uris("C:\neoEmbeddedDb")
                .build();

        factory = new SessionFactory(configuration, packages);

This works well, but now i want to browse the created database with the Neo4J Browser Tool. As i read, i have to expose my database via Bolt to be able to access it.

In the Neo4J Embedded documentation, they use the GraphDatabaseService and simply specify an additional bolt driver to expose the database:

GraphDatabaseService graphDb = new GraphDatabaseFactory()
                .newEmbeddedDatabaseBuilder( DB_PATH )
                .setConfig( bolt.type, "BOLT" )
                .setConfig( bolt.enabled, "true" )
                .setConfig( bolt.address, "localhost:7687" )
                .newGraphDatabase();

But unfortunately, i don't have this option when using the OGM SessionFactory. I tried to call the Configuration Builder with multiple URIs:

Configuration configuration = new Configuration.Builder()
                .uris(new String[]{this.databasePath.toUri().toString(), "localhost:7687"})
                .build();

But it seems to ignore the first URI (my file location) and instead creates the database in a temporary location.

The debug output logs a corresponding message to the console:

Creating temporary file store: file:/C:/Temp/neo4jTmpEmbedded.db2736315981519762299/database/

Can anyone explain how i can expose my embedded database via bolt or access it otherwise with the Neo4J Browser?

Many thanks!


Solution

With meistermeier's help i was able to create a real EmbeddedDatabase and connect my OGM to it. I added the Bolt connection options as i found them in the documentation. Now, the database is created and properly exposed via Bolt. I can connect with my Neo4J Desktop Windows Browser.

The final code is

BoltConnector boltConnector = new BoltConnector(_BOLT_CONNECTION_STRING);

GraphDatabaseService graphDb = new GraphDatabaseFactory()
        .newEmbeddedDatabaseBuilder(databasePath.toFile())
        .setConfig(boltConnector.type, "BOLT" )
        .setConfig(boltConnector.enabled, "true" )
        .setConfig(boltConnector.listen_address, "localhost:7687" )
        .setConfig(GraphDatabaseSettings.auth_enabled, "false")
        .newGraphDatabase();

registerShutdownHook(graphDb);

// connect OGM session factory to embedded database
EmbeddedDriver driver = new EmbeddedDriver(graphDb);
final String[] packages = new String[] {
        "Entity domain package",
};

factory = new SessionFactory(driver, packages);
1

There are 1 best solutions below

1
On BEST ANSWER

First of all: Neo4j-OGM is not in charge of any more complex database start besides creating a connection to it. Providing an instance of Neo4j with a custom configuration will make your application responsible for setting it up correctly.

Secondly: What you are trying to achieve is not possible. At least not with another server instance that serves the Neo4j Browser that can connect to any bolt protocol server once started. You can start an embedded instance and, if you really want this, open the bolt port. But there will no built-in Neo4j browser been deployed.

But after all let me explain why you see the temporary DB folder message: Within your setup snippets I can see two instances getting created: One by creating an explicit instance of the GraphDatabaseService and the other one through the configuration. The first one is obvious, the second one not so much. I just assume that you are not using the very latest version of Neo4j-OGM (3.1.8 at the moment of writing this answer).

The method/parameter uris is just for additional uris (prior 3.1.8) regarding causal cluster and routing. You would have to use uri with a single uri (either bolt://.... or file:///...) in your case. The behaviour of Neo4j-OGM in the case of an undefined uri is to fall back to an temporary embedded instance.

Another solution for this problem is to not provide a Configuration but an EmbeddedDriver instance for the SessionFactory. This can wrap the already configured GraphDatabaseService like new SessionFactory(new EmbeddedDriver(graphDatabaseService), ...). Then you would have the exposed bolt port but unfortunately not a running browser.