Pega Lock mechanism

​Hi,

In this post, I will share how Pega object locking works. If you find any information incorrect please let me know so I can revise it.

First of all, Pega uses both database level lock and Pega level lock. Database level lock is shorter durations (typically milliseconds), while Pega level lock is much longer (depends on user’s operation, but probably a few minutes). You can turn off Pega level lock using Optimistic locking strategy (explained later) but you can’t turn off database level lock. It is enabled in the infrastructure layer. From here on, I will explain about Pega level lock.

Pega level locking and database level locking.png

1. Pega level lock basics

When someone opens an assignment, the state becomes “Perform” mode and lock is obtained by the system. The lock information is inserted into “PR_SYS_LOCKS” table in the database and it is managed there. In this period, no other user can open the same assignment. When user submits or cancel the assignment, the state becomes “Review” mode and lock is released. The record is deleted from the table.

When someone tries to open an assignment that is already locked by other user, a message is shown as below. There is no way to unlock it.

error message locked by other session.png

If you try to open an assignment that is locked by yourself in other session, a different message is shown as below. This doesn’t happen a lot but it could when you log in to the system using the same ID from different browser or terminal. In this case, you are given an option to forcibly log out other session. Be noted, Pega locks are exclusive to one thread.

error message locked by the same ID.png

Locks can be also released when user logs out, or system restart. In other words, if user leaves assignments open and closes a browser by clicking the Windows close box, locks remain held by the requestor and prevents others from updating the work objects, for the next 30mins (see below “soft lock”), or until next system restart. A JavaScript event or function cannot detect the closing of the browser window. Hence, it is encouraged that user always do logging out operation properly. This rule applies to SSO environment too.

2. Soft lock vs Hard lock

Lock is effective for 30mins and then expires. After expiration, the lock becomes “soft lock” and other user can steal it thereafter. When other user opens the soft locked assignment, no warning is displayed and the user will not be even aware of its soft lock state. In the database, the same record in PR_SYS_LOCKS table is updated by the new user. Lock before expiration is sometimes referred to as “hard lock” as opposed to soft lock.

2-1. How things work

Below are the steps how hard lock / soft lock work in Pessimistic Locking strategy.

2-1-1. At 9:00am, user A opens an assignment in Perform mode. System inserts a new record into PR_SYS_LOCKS table. Since the default expiration period is 30mins, 9:30am is set to pxExpireDateTime property. For pxOwnerId property, user A’s requestor ID is stored. User A leaves his desk with assignment screen open.

2-1-2. Until 9.30am, no one can steal lock on this case.

2-1-3. At 9:31am, lock gets expired. The record in PR_SYS_LOCKS table still remains (it doesn’t get deleted by itself), but user A’s lock is no longer “hard-lock”. User B can now steal the lock. When user B opens an assignment at 9:31am, system updates pxExpireDateTime and pxOwnerId property of the same record. The expiration time is now set to 10:01am.

2-1-4. User A comes back to his desk at 9:45am. If he clicks Save or Submit button, he will see an error message on screen (as shown in the screenshot below) because he has already lost lock on this case. Regardless of user B’s situation (whether user B still has a lock or already submitted), this behavior will be the same.

2-1-4-1. If user A clicks Save button, below message is displayed.

2-1-4-1.png

2-1-4-2. If user A clicks Submit button, below message is displayed.

Q: After user A opens an assignment, if he clicks Save button, does the expiration time (pxExpireDateTime) get extended?

A: No, the expiration time is fixed at the beginning.

Q: If lock is expired and stolen by user B, user A will see an error message when he clicks Save or Submit button. How about when no one steals a lock but expired? For example, user A opens an assignment at 9:00am and clicks Save button at 9:31am. Will user A still see the error message because lock is expired?

A: No, the error appears only when the lock is stolen (updated) or released (deleted). If no one steals the lock, his lock record still exists in PR_SYS_LOCKS table as “soft-lock” state. User A should be able to save or submit without any issue.

2-2. How to change the expiration period

The default expiration period is 30mins, but you can change it. There are two places you can do this.

2-1. If you want to change it per individual case type, update Case Type rule.

2-2. If you want to apply the change system-wide, update Data-Admin-System (pega) instance.

  • If you update both of them, Case Type rule takes precedence over Data-Admin-System instance.

  • I would recommend that you take Case Type rule approach because it is a little complex to manage multiple environments by Data-Admin-System approach. For example, if you export and import Data-Admin-System instance from Dev to Prod, the production level will also be updated from 5 to 2 wrongly, so you shouldn’t. For someone to remember to update Data-Admin-System instance manually per environment is also painful. Case Type is a rule instance and it is migrated thru R-A-P without any complexities.

3. Pessimistic locking vs Optimistic locking

The Pega level object locking explained above is called “Pessimistic locking”, as opposed to “Optimistic locking”. In other words, Pessimistic locking means Pega level lock + database level lock while Optimistic locking means database level lock only (*). Optimistic locking approach was introduced in Pega 7. The default setting is Pessimistic locking.

  • “Allow one user” means Pessimistic Locking.
  • “Allow multiple users” means Optimistic Locking.
  • Technically speaking, even Optimistic locking will acquire a lock for a very short period of time when user clicks “Save” or “Submit” button. The duration is in milliseconds and closed to database level lock quickness. If you want to turn off Pega level lock completely, the only approach is to disable locking from Class Group (explained later in 5), but this is not recommended. Also, usually you don’t have to worry too much about collision if update occurs only from UI. If business requirements include update from service in the background and you want to avoid collision, you can build retry functionality using Queue Processor.
No Type Description Default
1 Pessimistic locking (Default locking) Pega level object locking using PR_SYS_LOCKS table. This approach prevents other users from overwriting data by locking the work object exclusively. A single user is allowed to open an assignment, and in that sense, this is a safer solution. Yes
2 Optimistic locking This approach doesn’t use PR_SYS_LOCKS table. Multiple users can open the same assignment at the same time. Whoever saves data latter overwrites data. Be noted that if latter users try to save or submit with updated data, they will be notified that someone else has already updated earlier. They need to reload the section to display the latest information first, and then re-enter their own updates. No

Personally I prefer Pessimistic locking from data consistency perspectives. If I were an end user, I would want to work on my assignment exclusively without worrying about potential that someone may steal and update the record in the middle of work. The disadvantage of Pessimistic locking is all other users must wait until the first user submits or cancels the assignment.

In Optimistic locking, when you click Save or Submit, system compares the last updated date time between one on memory (pyWorkPage) and database. If it is identical, that means there are no changes after you open the assignment, so no error. If one in database is newer, that means someone has already updated in the meantime and system displays a message (see below sample image). If you are using an activity to update a work object, make sure to update pxUpdateDateTime and pxUpdateCounter. Without these, system won’t know it is updated. For example, if you use Obj-Save method, it doesn’t update these properties. Use Process API such as Work-.UpdateWorkObject etc.

Notified of update message.png

This message doesn’t tell you which exact properties are updated. If there are many properties on screen, it may be painful to find out the differences (it could be even no differences, someone might have just saved it with no changes). Which locking strategy to choose is up to the nature of your application and you are advised to discuss which approach is more suitable with business users.

4. Lock mechanism with case hierarchy

Case hierarchy affects lock mechanism in a certain way. I have posted a separate article. Please see https://support.pega.com/discussion/lock-mechanism-case-hierarchy.

5. Allow locking settings at Class Group / Class level

I explained how to configure Pessimistic locking or Optimistic locking above but actually there is one more prerequisite setting in a lower level - Class Group or Class form. For Work instance, locking must be enabled at Class Group form. For Non-Work (≈Data) instance, locking must be enabled at Class form.

5-1. Work (Class Group)

When you define a case type, this checkbox is turned on by default. You can always keep it checked. By checking this off, Pega level lock is disabled but you can also turn it off by using Optimistic locking. Therefore, there is no advantage in check this off.

5-2. Data (Class)

Unlike Work, Data has “Locking” tab in the Class form. When you define a data type and configure local data storage, this checkbox gets turned on by itself. When it is checked, Data instance locking works in similar manner as Work and PR_SYS_LOCKS table is used. The difference between Work and Data is, Data instance is not locked when assignment is opened, but it is locked when data row is being edited (ex. inline row editing). If you check this off, it behaves like Optimistic locking.

  • If Allow locking is checked for Data instance, when someone starts editing the record, lock is obtained. A message is shown as below if you try to update a locked instance.

6. Controlling lock in an activity

When end user operates on screen, object locking is managed by the system automatically, but when we use activity, lock must be controlled by developer with care.

In Obj-Open method or Obj-Open-By-Handle method, there are two checkboxes - “Lock” and “ReleaseOnCommit”. If you check “Lock”, lock is obtained in this method. If you check “ReleaseOnCommit”, lock is released when Commit method is executed. When you are planning to update the instance, check both of them. In most cases, check / uncheck both of them. In the unusual case, you may want to check only “Lock”, and not “ReleaseOnCommit”, to retain the lock after Commit (perhaps because additional changes are expected). Then you should manually release lock later using the Page-Unlock method etc (*).

Another method, Obj-Refresh-And-Lock also can obtain a lock. While Obj-Open and Obj-Open-By-Handle grabs data from database every time, Obj-Refresh-And-Lock is used when you already have a page on clipboard. If the object is not locked, this method acquires a lock and ensures that the version you have on a clipboard page is the same as or newer than the version currently in the database. If the object is locked, this method has no effect.

  • I wouldn’t recommend to implement programmatic solution but if you need to manually release a lock, do so using either of below approach.
No Approach Type
1 Pege-Unlock Method
2 Work-Cover-.WorkUnlock Activity
3 System-Locks.ReleaseLock Activity

Thanks,

Thanks for posting, I found it very much helpful. I have one question regarding the 6th point.

What type of failure it would cause if the lock is not release?

@KenshoTsuchihashi Are the lock records maintained only in the database or are they cached as well for better performance

@KenshoTsuchihashi - Great Article.

@KenshoTsuchihashi Can u suggest any resources to look into which categorically addresses the need of “Allow one user” locking strategy with the help of a Simple Business Transaction. I am unable to comprehend that if multiple users are allowed to work on a Case then how will it lead to a business impact or any such problems ?

@PreshM59

In my opinion, I like Pessimistic locking better than Optimistic locking because Pessimistic locking ensures data integrity by limiting case accesses. If I am an end user, I don’t want to be interrupted by someone updating the case in the middle of work. Having said that, this is just my personal preference and you can choose either approach depending on the business needs. Both locking strategies are provided as out-of-the-box functionalities and there shouldn’t be any technical difficulties.

Thanks,

@KenshoTsuchihashi Must thank you for such an informative and elaborate article. Its helpful for beginners as well as experienced developers !!!

Hi @KenshoTsuchihashi

This is a very great article.

What is the lock mechanism used in the rules? I mean, I have opened one rule and I am editing the rule, and simultaneously, on the other side of the same rule, you are trying to checkout and modify the rule. You or other members don’t have a rule checkout option. What is the mechanism is used for locking ?

Note:I can understand that this article is applicable only to work objects, not for the rules.

For Example:

If we assume a Pessimistic or Default locking mechanism, the private edit option also should not come.

If we assume an Optimistic Locking mechanism, I can’t see the rule check-out option for another user for the same rule.

I just want to clear up my doubts, nothing much.

I am eagerly waiting for your valuable input. Do we have any updates on this? @KenshoTsuchihashi

Thanks,

Ashok

@KenshoTsuchihashi Thanks for posting

I came across with this strange SQL errors :

java.sql.SQLIntegrityConstraintViolationException / OR ***** unique constraint (PR_SYS_LOCKS_PK) violated

it is to do with when pega does INSERT into pr_sys_locks with duplicated primary key value (on pzinskey) . After some research I guess this happens in somewhere in our activities that didn’t handle the locking properly.

I guess this may be a failure you would get if lock is not release and another user tries to commit the same case.

Hi Paramveer,

Let’s say, developer manually opens a work object using Obj-Open or Obj-Open-By-Handle method with “Lock” checked, but “ReleaseOnCommit” not checked, and activity ends after committed. Sample code is shown below (bad practice).

In this case, it behaves just like end user closes Window in the middle of perform mode without properly logging off - any changes saved to the database is valid, but lock won’t get released (lock is held by the user who ran the activity). That means, other user won’t be able to open the assignment by perform mode for the next 30mins (until hard lock expires), or system restarts. This isn’t an ideal situation, and hence it is advised to check both “Lock” and “ReleaseOnCommit” checkbox.

Hope this clarifies your question.

@AnkujJ44

Lock used to be maintained in the database table only. However, from version 6.3 SP1 onwards, locks are tracked in two places - database table and in-memory cache of the node that the requestor has the lock. The lock cache mechanism was introduced for the better performance.

If you want to, you can disable this lock cache by setting false for below Dynamic System Settings (It doesn’t exist by default, and you have to create it yourself). That said, typically you wouldn’t need to disable it unless it’s for troubleshooting / debugging purpose.

  • Owning Ruleset: Pega-Engine
  • Setting Purpose: prconfig/database/lockcache/enabled

Thanks,

@KenshoTsuchihashi We are facing an error in Pega 8 in releasing a lock when the node is down. Is it caused by lockcache maintained in node level? Even though the user clicks “end other session to release lock”, Pega is not able to release the lock. This happens only when the node goes down as the user is working on the case

@KenshoTsuchihashi Thanks a lot for the details. It is very useful and informative

@KenshoTsuchihashi Thanks for sharing useful insights. The explanation is very detailed and clear.

@PreshM59 well let’s take a scenario where u need to get approval from some higher authority, well u routed it to a workbasket. Well who ever in that workbasket can pick and approve it. In this scenario let’s assume there is no locking provided at a time 2 members picked the assignment for various reasons one approved & other rejected (what should happen to that case). So we need to have locking defined on case so when someone working on case we need to show other people that it’s already been processing

@KenshoTsuchihashi This is a great article, many thanks!

@New2740 @muppl @hallm3

I was able to replicate the same issue at my end. I’ve involved our support and engineering team to discuss whether this is a defect or not. Let me first outline how object locking works in a multi-node environment. Let’s say, we have two app nodes. Two users are connecting to a different app node and performing a different work object respectively - taro.yamada locks W-1 on App 1, and jiro.yamada locks W-2 on App #2, shown as below.

  1. At this moment, two records are created in PR_SYS_LOCKS table as below. Each record holds information about who locks which work object, on which app node.

  1. Now, let’s shut down Tomcat on App 1. This operation can be either normal shutdown or killing the process abnormally. In PR_SYS_LOCKS table, one more record “Server Process” is created temporarily but let’s ignore this guy for now. More importantly, taro.yamada’s record remains in the table although his browser is already unresponsive. It can’t be deleted by itself.

  1. Start up Tomcat on App 1. During its startup process, system will delete App 1’s dead record. Now taro.yamada’s record is cleaned up.

  1. If taro.yamada refreshes browser and connects to the system, load balancer will forward the request to the same App 1 (cookie stickiness). taro.yamada can again peform the same work object W-1 without any issue. A new record is created in PR_SYS_LOCKS table just like the beginning. This is the working scenario.

Now, get back to your question. If you are seeing below “This case is locked by you in another session…” message, I believe taro.yamada has accessed App #2 after App 1 is shut down. As we saw above, since taro.yamada’s dead record remains even after App 1 is shut down, if he tries to perform W-1 on App #2, it makes sense for system to show this message.

Next, what happens if taro.yamada clicks “End other session to release lock” button? When clicked, Review Harness is displayed as below. Looks like the operation is successful.

However, it is not. When I click “Begin” again to perform W-1, the same message is shown again. I can’t seem to release lock! There was no error logged in the PegaRULES log file.

  • Direct cause

When “End other session to release lock” button is clicked, below WorkUnlock activity is triggered. In this activity, unlock engine API gets called. unlock API can terminate the other requestor and release the lock, only when the node is active. In our scenario, the node is dead and hence, unlock failed.

This is the current specification and expected behavior at moment. I’ve submitted an enhancement request (FDBK-91895).

Thanks,