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

What about transactions? #111

Open
crocodile2u opened this issue Jun 21, 2019 · 9 comments
Open

What about transactions? #111

crocodile2u opened this issue Jun 21, 2019 · 9 comments

Comments

@crocodile2u
Copy link

Atomicity of operations is a vital part of any application. How do you handle transactions? I reckon, one can just make it this (simplified):

$conn->query("BEGIN")
  ->then(function() use ($conn) {
    $conn->query("INSERT/UPDATE ...")
      ->then(function() use ($conn) {
        $conn->query("COMMIT");
      });
    });

Would be nice to have this covered in docs.

@clue
Copy link
Contributor

clue commented Jun 28, 2019

@crocodile2u Looks about right 👍 Do you feel there's something that needs to be done here? PRs would be much appreciated :shipit:

@crocodile2u
Copy link
Author

Yeah, I think a transaction() method would be great. It can accept a callback which is then executed within a DB Transaction and return (surprise!) a Promise

@jsor
Copy link
Member

jsor commented Jul 3, 2019

As this library i quite low-level, i think such a method would be better suited in a package providing a higher-level abstraction on top of this library, think of something like Doctrine DBAL which provides an abstraction on top of PDO.

Other than that, documentation improvements are very welcome 👍

@langabi
Copy link

langabi commented May 17, 2020

Just on transactions: I think I'm right that the code above has a (possibly) unexpected behaviour. Specifically, while the database is handling BEGIN, other queries may be sent to the database from elsewhere in the PHP script, and then surprise: these will be executed within the scope of this transaction. That is, the entire PHP script's queries will be in the transaction, and not only the queries in the specific chain of ->then() calls above.

I think the only way to really isolate a specific thread of ->then() calls in a transaction that is not shared by the rest of the script, is to open an entirely separate connection instead of running BEGIN.

Let me know if I'm somehow wrong on this; otherwise, I thought worth mentioning for future readers!

@WyriHaximus
Copy link
Member

When we do transactions it should be very clear that we start a transactions, preferably from this package's API directly

@crocodile2u
Copy link
Author

Specifically, while the database is handling BEGIN, other queries may be sent to the database from elsewhere in the PHP script, and then surprise: these will be executed within the scope of this transaction

That's a nasty behavior. That place elsewhere in the PHP script might send it's own BEGIN. MySQL doesn't handle nested transactions, therefore it's going to result in an error.

@pavarnos
Copy link

Could this be something added to the LazyConnectionHandler? transaction(callable) could create a new connection internally to run the query inside the callable, then close the connection after?

@gizahNL
Copy link

gizahNL commented Aug 4, 2020

Specifically, while the database is handling BEGIN, other queries may be sent to the database from elsewhere in the PHP script, and then surprise: these will be executed within the scope of this transaction

That's a nasty behavior. That place elsewhere in the PHP script might send it's own BEGIN. MySQL doesn't handle nested transactions, therefore it's going to result in an error.

Therefore any transaction should probably occur in its own connection.
It could be coded in a way that transaction start opens a new connection and returns a handle to it, and the final commit then closes the handle again.
Could also be handled internally by returning a transaction ID on which work is to be done and providing a transaction id variable in the query method(s)

@nstwfdev
Copy link

nstwfdev commented Dec 21, 2022

Hello there, 2.5 years later since last message

I created a simple wrapper around ConnectionInterface class, whats about that idea?

https://github.com/nstwfdev/mysql-connection

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

No branches or pull requests

8 participants