MariabDB synchronous UPDATE query

Viewed 83

I'm working with PHP and MariaDB and I run into a problem.

I update a value to multiple rows, and then SELECT there rows to make a new calculation the data for another task.

The problem here that I get the wrong number. I guess that the MariaDB has not finished the UPDATE query, but it return the finished flag to PHP and then the PHP proceeds the SELECT query. [I just guess]

I open to any idea. If I'm wrong, please correct me.

Thank you for sharing

This is my code

$modelAdminOrderBidSys = $this->load->model('Admin\Order\BidSys');

$acceptedItem = typeCast($modelAdminOrderBidSys->getItem($cartItemId));

if (!$acceptedItem) {
    return array(
        'result' => 'error',
        'message' => 'Cannot find item #' . $cartItemId
    );
}

$acceptedItem['lastOffer'] = $acceptedItem['offer'];
$acceptedItem['accepted'] = 1;
$acceptedItem['isBot'] = 0;

$modelAdminOrderBidSys->updateItem($cartItemId, array2object($acceptedItem));

$cartItems = typeCast($modelAdminOrderBidSys->getItems($acceptedItem['cartId']));

$accepted = 1;
$total = 0;
$offer = 0;
$lastOffer = 0;

foreach($cartItems as $cartItem) {
    if ((int)$cartItem['accepted'] < 1) {
        $accepted = 0;
    }

    $total += (float)$cartItem['total'];
    $offer += (float)$cartItem['offer'];
    $lastOffer += (float)$cartItem['lastOffer'];
}

$postField = new \stdClass();
$postField->accepted = $accepted;
$postField->total = $total;
$postField->offer = $offer;
$postField->lastOffer = $lastOffer;

$modelAdminOrderBidSys->updateCart($acceptedItem['cartId'], $postField);
1 Answers

It sounds like your SELECT transaction starts before the UPDATE has committed. Try changing the transaction_isolation (in config) / tx_isolation (at runtime with SET GLOBAL) to READ-COMMITTED. Default is REPEATABLE-READ.

Related