-
-
Notifications
You must be signed in to change notification settings - Fork 520
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
Transactions support #142
Comments
There is a reason that SQLx takes in a I think, probably, the SeaORM API can take a All in all, I think transaction in an async context is a complex matters that requires more investigation. |
Transactions are often used to lock to a certain state (isolation) to be sure that there are not changes between select queries. How do you cover that case with a I can try to see if I can fit inside sqlx apis with a wrapper, if it covers all scenarios |
You are right. |
I've analyzed further the situation. I think we want to give users consistent APIs, therefore a function that accepts &DatabaseConnection should work both inside and outside transactions. To do so I see 2 ways:
Both ways permits both transaction closure style (like Let's analyze the 2 ways: Connection trait Other problems I've found with
|
Just to inform you, I've found a lifetime problem with my transaction implementation. |
My implementation allows you to return anything that impl Error, wrapper in an error Type that permits you to know if the error happened opening the transaction |
Yeah, just noticed that, haha. I wasn't expecting it to be a generic argument (in the test it's set to DbErr, which I missed). |
I managed to work around the lifetime issue by cloning the Arc just before starting the transaction, so thankfully that's a non-issue for me. I do have an additional question, though. Is it possible to accept both a normal connection and a transaction connection in a single function? I assume it would use ConnectionTrait somehow, but it doesn't seem to like that. |
Yes, use a generic over trait ConnectionTrait |
Hm. Well, I guess my inexperience with Rust is the issue, here. I'll see if I can't figure out how to actually do that. Anyway, thanks for the tip. Seems like you thought of everything, already, haha |
I've got a suggestion, if it's in scope for this change. Could there be support for running tests inside of a transaction that always rolls back once the function has completed? Perhaps a procedural macro? The current way to do this is to start a transaction in each test, and always return Rollback in an Err, but this is a bit awkward |
The theme of 0.3 have been designated as "Transaction". This will be the focus of our effort in October. |
Coming from #118, I start a new issue con go on with the analisys:
What @tyt2y3 proposed:
It can be a starting point, if we're able to maintain sea-orm typing.
What I mean is, sqlx Connection::transaction takes an fn(&mut Transaction), we would need to wrap it up so inside the function we can still use our DatabaseConnection type.
And it's kind of an overkill IMHO, Transaction is only a sugar wrapper over a normal connection, where a START TRANSACTION has been executed.
I would prefere an approach like this: https://docs.rs/mysql_async/0.28.0/mysql_async/struct.Conn.html#method.start_transaction
where Transaction is simply a tiny wrapper over Connection and can be used in any method that takes
&Conn
because itimpl Deref<Conn>
About isolation levels, mysql and postgres are quite similars:
https://dev.mysql.com/doc/refman/8.0/en/set-transaction.html
https://www.postgresql.org/docs/9.5/sql-set-transaction.html
Sqlite has transactions too, it has a little different dialect, like BEGIN [TRANSACTION] instead of START TRANSACTION, and isolation levels have to be set using the PRAGMA keyword, like https://www.sqlite.org/pragma.html#pragma_read_uncommitted
The text was updated successfully, but these errors were encountered: