A
A
Alex Wells2017-08-28 18:52:04
MySQL
Alex Wells, 2017-08-28 18:52:04

Mysql block transaction by foreign key?

Hello. There is a code, in php + laravel + mysql, that behind the scenes executes a transaction in mysql:

DB::transaction(function () use (&$game, &$bet, $items, $user) {
  $bet->user()->associate($user);
  $bet->game()->associate($game);
  $bet->save();
  $this->inventoryService->move($items, $bet);
  $game->save();
});

Actually in the bets table there is a game_id field with a foreign key to the games table. Is it possible to make it so that during the lock of the games table, by another transaction, the first one throws an error?
You need it like this:
game_transaction2 start
bet_transaction1 start
bet_transaction1 ERROR
And now like this:
game_transaction2 start
bet_transaction1 start
bet_transaction2 stop success
game_transaction2 stop success

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2017-08-29
@AlexMaxTM

Honestly, it is not clear why you need to exit with an error. If the first transaction is still running, then the second one will simply wait for its completion and work later. From the point of view of the DBMS, there is no error, so it cannot be caught. An error can occur if only a "deadlock" (deadlock) has occurred, then one of the transactions is fired and you can get a message about it from the DBMS.
But if you really want to receive a message, then first check if the table is not locked before starting the transaction, and if so, issue a message, and do not start the transaction.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question