neo4j-labs / neo4rs

Neo4j driver for rust

Home Page:https://docs.rs/neo4rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Where do we initialize the connection? Is it in a global variable? If not, should we create a new connection each time we need to run a query? And, are these connections auto managed by a pool?

tausifcreates opened this issue · comments

I'm using Axum and keep a neo4rs::Graph in the AppState. The connection is handled under the hood, but from what I recall reading the Go driver, each request starts a new session. The sessions use a connection pool.

I'm using Axum and keep a neo4rs::Graph in the AppState. The connection is handled under the hood, but from what I recall reading the Go driver, each request starts a new session. The sessions use a connection pool.

Hey! Thanks for sharing the insight. I am using a global varibale, OnceLock to be precise.

Graph should be you entrypoint and you should keep that one in a singleton-ish place, like the axum state as @jifalops mentioned. A static OnceLock is also ok, depending on your application.

Under the hood, a connection pool is used (and its size can be configured with max_connections). Every invocation of run, execute, or start_txn will get a new connection from the pool (possibly waiting until one is available).

We don't have sessions in the Rust driver yet.

Graph should be you entrypoint and you should...

@knutwalker Thanks for the insight :)

@knutwalker What is a session, to be exact, in the context of Neo4j driver?

@tausifcreates

The definition of a session is "A causally linked sequence of transactions.".

What that means is, that you've got an abstraction that allows you to run multiple transactions after one another in a way that enforces causal consistency on the database cluster, that is, make sure your queries are consistent to the rules that a Neo4j cluster provides (for example, you typically want to read your own writes). You need to work and handle so called bookmarks to do that and a session is an abstraction where this handling is done for you, by the driver. A session might be pinned to a single connection, so all queries run over the same connection (In neo4rs, every query or transaction acquires a new connection from the pool, which might be the same as before, or it might be a new one).

We don't support bookmark support in neo4rs yet, so we don't have to have sessions as well, but it's somewhere on the roadmap. For now it means, when you use neo4rs and have a cluster on the other side, we cannot enforce causal consistency and you might not read your own writes.

@knutwalker Thank you so much for taking the time to explain very clearly!

Also many thanks for the awesome work on neo4rs. For this crate, I could move my backend to Rust ecosystem:)

Thanks for the feedback, that's great to hear :)