neo4j-labs / neo4rs

Rust driver for Neo4j
https://docs.rs/neo4rs
197 stars 58 forks source link

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? #107

Open tausifcreates opened 1 year ago

jifalops commented 1 year ago

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.

tausifcreates commented 1 year ago

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.

knutwalker commented 12 months ago

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.

tausifcreates commented 12 months ago

Graph should be you entrypoint and you should...

@knutwalker Thanks for the insight :)

tausifcreates commented 8 months ago

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

knutwalker commented 8 months ago

@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.

tausifcreates commented 8 months ago

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

tausifcreates commented 8 months ago

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

knutwalker commented 8 months ago

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