13. Other Tidbits
* Optimistic Locking and QForms
<< Previous   |   Back to Main   |   Attribute Overriding >>

View Source
will open in a new window
Integrating Optimistic Locking into QForms
In Section 2, we showed how by using the TIMESTAMP column types, Qcodo will generate code to handle Optimistic Locking. In this example, we take this a step further to illustrate a more functional approach to utilizing Optimistic Locking in your web based application.

In our example below, we have the same Person object instantiated twice. This is supposed to mimic two users on two different computers trying to edit the same Person object at the same time.

(Note: on some database platforms, including MySQL, no SQL UPDATE will be performed unless the data has actually been changed. It's recommended that you make a change to either the First Name or the Last Name before hitting Save in order to see this example in action.)

As you can see, the Optimstic Locking functionality will allow both "users" to view the data. But once one user tries to update one of the Person objects, the other Person object is recognized as "stale" (because of a TIMESTAMP mismatch). Any subsequent call to Save on the "stale" Person will throw an exception. We catch this QOptimsiticLockingException in our QForm in order to present a more graceful response to the user, allowing the user the option to override the changes made by the previous Save call, forcing the update.
Current Name and Timestamp values in the database for this PersonWithLock object:
Alfred Newmangggyyyyad  |  2024-01-26 00:30:45

PersonWithLock Instance #1

2024-01-26 00:30:45

PersonWithLock Instance #2

2024-01-26 00:30:45