We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How can i implement optimistic locking while updating same data at the same time by more than two users using PhalconPHP?

Please consider these example.

User A and User B are updating same records at the same time. User A updated the records while User B is also updating records at same time. How can i notify User B that these records are already updated by User A?

How can i implement Concurrency for CRUD operation using PhalconPHP?

Websockets i guess. When some user will open it then it he will lock it for him to edit only, if someone other will try to open it - then he will get notification. And websockets are just for real-time so we don't need ajax which can be too slow.

edited May '16

Thanks for reply @Jurigag Ślawski.

That is okey. we can use Websockets. But my concern is only for PhalconPHP. Does PhalconPHP provide concurrency for CRUD? Or Does PhalconPHP will implement concurrency handling in next upcoming versions?

edited May '16

Phalcon is framework (a set of classes, methods, functions etc.) built on top of PHP. Like I said - the purpose of application server (PHP) is to handle business logic, and database system has purpose to take care about your data in terms of concurrency etc.

When you say 'records' - you mean database data, right? If that's what it is then simply your RDBMS enginge will handle concurrency, which basically is what all database systems out there are up to - to provide stable data storage for your application.



11.6k
edited May '16

you can (you must) use prepared transactions to encapsulate your query in your code, and if you use a DB like postgresql, it have a powerfull system of lock in the backend, other db have too I suppose. The db locks system combined with prepared transaction make the risk you get unconsistancy near zero, as I know...

edited May '16

@stamster,

That i know. My question is that, How can i implement optimised locking using phalcon? Is there any built in classes for that? Cosider this example.

User A and User B are updating same records at the same time. User A updates while User B has old records. when User B is update records then User A's records is overrite.How can i notify User B that the records which User B wants to update is already updated by User A. I only want to use PhalconPHP. How can i do that?

@Graphmatic,

I always use transaction while operating on Data. My question is simple, How can i implement Optimistic locking using PhalconPHP?Is there any built in classes for that?

Cosider this example.

User A and User B are updating same records at the same time. User A updates while User B has old records. when User B is update records then User A's records is overrite.How can i notify User B that the records which User B wants to update is already updated by User A. I only want to use PhalconPHP. How can i do that?



11.6k

you can load the record as user B is entering your view, then, before saving mods done by this user B, reload an instance and compare with the previously loaded one, if there is any diff, that means that your row have been modified by another user. You can also implement some mechanism to log which user modify your record if you need so. If you need to inform user B another one is modifying the record before he submit anything, add a "currently editing" column in your table, and swith this bool on as soon as a user enter the modifying form (and switch it off when saving), so any other user opening this same form for the same record will see that this record is currently edited by someone else



11.6k

there is also a "keepSnapshots()" property on models that can be suitable for your purpose...

@Graphmatic,

Thank you for the reply. I have now implemented concurrency mechanism by your help. Your help much appricieted.