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

Add SQL Transaction #35

Open
AkmalFairuz opened this issue May 22, 2020 · 6 comments
Open

Add SQL Transaction #35

AkmalFairuz opened this issue May 22, 2020 · 6 comments

Comments

@AkmalFairuz
Copy link

AkmalFairuz commented May 22, 2020

Is your feature request related to a problem? Please describe.
I tried to make Payment System like EconomyAPI (/pay).
A: doing pay to B
B: receive pay from A
It need 2 query (
Reduce money A
Add money B )
, but when 1 query executed the server crash, A money reduced and B money not added.

I need startTransaction function and callback if query failed.

@SOF3
Copy link
Member

SOF3 commented May 22, 2020

For now, your specific case can be done like this:

-- # :payer string
-- # :recipient string
-- # :amount int
UPDATE money SET amount = amount * IF(player = :payer, 1, -1) WHERE player IN (:payer, :recipient)

This is a very ugly hack, but before transactions get implemented, this method can successfully squeeze your queries into one.

@AkmalFairuz
Copy link
Author

Hmm, Can you add LOCK TABLE & UNLOCK TABLE ?, Query will conflict when multi server using one MySQL Server

@AkmalFairuz
Copy link
Author

AkmalFairuz commented May 23, 2020

For now, your specific case can be done like this:

-- # :payer string
-- # :recipient string
-- # :amount int
UPDATE money SET amount = amount * IF(player = :payer, 1, -1) WHERE player IN (:payer, :recipient)

This is a very ugly hack, but before transactions get implemented, this method can successfully squeeze your queries into one.

It only example. But i can't do this when result from query SELECT and the result must executed by code

@SOF3
Copy link
Member

SOF3 commented May 23, 2020

Right now the best solution is to CREATE PROCEDURE or CREATE FUNCTION.
Adding support for multiple queries breaks quite a lot of internals and is not trivial to implement.

@matcracker
Copy link
Member

The problem could be simply resolved using AwaitGenerator.

  1. Wait until query reduced money to A
  2. Then add money to B.

@SOF3
Copy link
Member

SOF3 commented May 28, 2020

The problem could be simply resolved using AwaitGenerator.

1. Wait until query reduced money to A

2. Then add money to B.

This has nothing to do with async. It is about the atomicness of transactions at the server side.

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

3 participants