# Trying to avoid concurrency and race condition problems while saving data

**URL:** <https://discourse.cakephp.org/t/trying-to-avoid-concurrency-and-race-condition-problems-while-saving-data/11677>\
**Category:** Need Help\
**Created:** [November 14, 2023, 2:56pm UTC](https://discourse.cakephp.org/t/trying-to-avoid-concurrency-and-race-condition-problems-while-saving-data/11677 "2023-11-14T14:56:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Herzberg](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Herzberg](https://discourse.cakephp.org/u/Herzberg)\
**Post date:** [November 14, 2023, 2:56pm UTC](https://discourse.cakephp.org/t/trying-to-avoid-concurrency-and-race-condition-problems-while-saving-data/11677/1 "2023-11-14T14:56:07Z")

</div>

Hello, i am trying to avoid concurrency and race condition in my cakephp 4 app while saving data that is edited by many users at the same time. I am trying to do it using transactions: $this-\>Users-\>getConnection()-\>begin(); / $this-\>Users-\>getConnection()-\>commit(); and rollback: $this-\>Users-\>getConnection()-\>rollback();

But how can I lock all relevant rows that are defined in $data and $data2? Means i have to lock:  
‘Connlists.Conns’,‘Connlists’, ‘Autolists’ for $friend and $friend2… How can i do it? And btw. what happens if i use transactions to lock rows in one method but not in another? Can i write even if its locked? Ah and is \>getConnection() out of touch in cakephp4? I am in UsersController…

```auto

$this->Users->getConnection()->begin();

try {
    $friend = $this->Users->findBySlug($slug)
        ->contain([
        'Connlists.Conns',
        'Connlists',
        'Autolists'
    ])
        ->firstOrFail();

    $id = $friend->id;

    $friend2 = $this->Users->get($this->request->getAttribute('identity')
        ->getIdentifier(), [
        'contain' => [
            'Connlists',
            'Connlists.Conns',
            'Autolists'
        ]
    ]);
    
    $data = [
        
        'autolist' => [
            'id' => $x,
            'feq' => $f,
            'frnt' => $s
        ],
        
        'connlist' => [
            'conns' => [
                [
                    'id' => $1,
                    'autolist_id' => $2,
                    'connslist_id' => $3,
                ]
            ]
        ]
    ];
    
    $data2 = [
        
        'autolist' => [
            'id' => $x2,
            'feq' => $f2,
            'frnt' => $s2
        ],
        
        'connlist' => [
            'conns' => [
                [
                    'id' => $x1,
                    'autolist_id' => $x2,
                    'connslist_id' => $x3,
                ]
            ]
        ]
    ];

    $friend->connslist->friends[] = $friend;
    $friend2->connslist->friends[] = $friend2;
    $friend->setDirty('conns', true);
    $friend2->setDirty('conns', true);
    $friend->setDirty('autolist', true);
    $friend2->setDirty('autolist', true);
    $friend->setDirty('connslist', true);
    $friend2->setDirty('connslist', true);

    $friend = $this->Users->patchEntity($friend, $data, [
        'associated' => [
            'Autolists',
            'Connlists.Conns' => []
        ]
    ]);
    $friend2 = $this->Users->patchEntity($friend2, $data2, [
        'associated' => [
            'Autolists',
            'Connlists.Conns' => []
        ]
    ]);

    if ($this->Users->save($friend) && $this->Users->save($friend2)) {
        $this->Users->getConnection()->commit();

        $this->Flash->success(__('Approved.'));
        return $this->redirect([
            'action' => 'view',
            $friend->slug
        ]);
    } else {

        $this->Users->getConnection()->rollback();
        $this->Flash->error(__('Not approved.'));
    }
} catch (\Exception $e) {
    
    $this->Users->getConnection()->rollback();
    $this->Flash->error(__('An error occurred.'));
}

```

any idea?

best,

Herzi

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [November 14, 2023, 4:23pm UTC](https://discourse.cakephp.org/t/trying-to-avoid-concurrency-and-race-condition-problems-while-saving-data/11677/2 "2023-11-14T16:23:54Z")

</div>

You could consider not handling the db actions during the request. Instead, make the request add the changes to a queue of change requests then send a response saying that the changes will be made.

Then you can make a background process that works through the edits on the queue one at a time.

You can capture any errors and let the user know if/when their edits were completed.

I’m sure this is a common design pattern. There are probably even existing tools you could leverage if you didn’t want to write the code.

---

<div class="post-metadata">

**Author:** ![jarekgol](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jarekgol](https://discourse.cakephp.org/u/jarekgol)\
**Post date:** [November 26, 2023, 12:34pm UTC](https://discourse.cakephp.org/t/trying-to-avoid-concurrency-and-race-condition-problems-while-saving-data/11677/3 "2023-11-26T12:34:52Z")

</div>

Maybe this will help? [Query Builder - 4.x](https://book.cakephp.org/4/en/orm/query-builder.html#adding-locking-statements)
