cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Spring Data Neo4j Select database at runtime using OGM Session

I am trying to implement a Spring application which is connected to a single neo4j instance(4.0+) with multiple databases.
I would like to dynamically select a database at runtime to perform my action in using an OGM Session as I find the save() method convenient for my use case.
I successfully implemented this by using SessionFactory:

public SessionFactory createSessionFactory(String database) {
	Configuration configuration = new Configuration.Builder()
			.uri(properties.getUri())
			.credentials(properties.getUsername(), properties.getPassword())
			.database(database)
			.verifyConnection(true)
			.build();
				
	return new SessionFactory(configuration, "my.package.domain");
}

And opening a Session in every method of my custom Repository:

public void createNode(String database, String param) {
	SessionFactory sessionFactory = createSessionFactory(database);
	Session session = sessionFactory.openSession();
	session.save(new MyNode(param));
	sessionFactory.close();
}

The process of creating a SessionFactory(which also creates a Driver ?), opening a Session and closing it after I've finished using it for every Repository method seems a bit complex. Am I doing this right ?

I also looked at this article https://medium.com/neo4j/reactive-multi-tenancy-with-neo4j-4-0-and-sdn-rx-d8ae0754c35 about multi-tenancy and was wondering if it was the way to go as I've never used it.
I should note that we need an "individual" database for each user as well as a "group" database to save the data of multiple users. Any entity saved in an "individual" DB should also be saved in the "group" DB associated with the corresponding user. I can see in the article that we statically create UserDetails with associated role. Is it possible to do it dynamically so that the database(s) on which the action should be performed is automatically chosen based on the user sending the request ? For that matter, we plan to save the databases info (an instance IP if we decide to have multiple ones and a database name OR a role if we use multi-tenancy) and the associated users using a "central database" (probably a relational DB).

Thank you for your help!

0 REPLIES 0