Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarity on transactions #139

Open
jebreuer opened this issue Jun 26, 2023 · 3 comments
Open

Clarity on transactions #139

jebreuer opened this issue Jun 26, 2023 · 3 comments

Comments

@jebreuer
Copy link

jebreuer commented Jun 26, 2023

Hi all!

I just started using surrealdb and surrealdb.js with deno for a backend service. I have one instance of Surreal that is being passed around.
Yesterday I used transactions for the first time. No issues so far. However, I asked myself the question how multiple concurrent transactions are supported by surrealdb.js. From what I've read and what I understood there's only one WebSocket connection that is used to handle all the communication with surrealdb.

Let's have a look at the (shortened) example from the official surrealdb docs:

(A1) BEGIN TRANSACTION;
(A2) CREATE account:one SET balance = 135,605.16;
(A3) CREATE account:two SET balance = 91,031.31;
(A4) UPDATE account:one SET balance += 300.00;
(A5) UPDATE account:two SET balance -= 300.00;
(A6) COMMIT TRANSACTION;

And add to that a second transaction:

(B1) BEGIN TRANSACTION;
(B2) CREATE account:three SET balance = 99,000.00;
(B3) CREATE account:four SET balance = 666,666.66;
(B4) UPDATE account:three SET balance += 200.00;
(B5) UPDATE account:four SET balance -= 900.00;
(B6) COMMIT TRANSACTION;

Let's say I start with A1 and execute until A3. New work gets introduced to the backend service and we start with B1 to B3 in a different context but still the same process. Then we continue with A and go from A4 to A6 and end the transaction. From my understanding that would mean that also B2 and B3 get committed which is certainly not intended by me as a programmer and as soon as B4 to B6 executes we end up with a weird state(?).

I am a little confused about what to expect here and what the actual behavior is. Any help on getting a proper understanding regarding transactions in the context of surrealdb.js would be highly appreciated.

When searching this repo for transactions I only found this (closed) PR:
#56
There @tobiemh mentioned something about "atomic transactions" which I actually do not understand.

Kind regards
Jens

@kearfy
Copy link
Member

kearfy commented Jul 17, 2023

Hi Jens,

transactions are built to ensure that all queries are succesfully executed (committing a transaction), or to view how a change would affect the database (cancelling a transaction). You can only execute these transactions in go, not in separate queries.

Does that help? Let me know if anything is unclear!
-- Micha

@terrablue
Copy link

terrablue commented Sep 10, 2023

Hi, are you planning to allow separate queries in a transaction, like the postgres package with sql.begin?

@kearfy
Copy link
Member

kearfy commented Nov 28, 2023

Heya! This is something we'd like to add in the future, but to add such functionality, it needs to be introduced in SurrealDB itself first :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants