Posted on
🔒 Understanding Uniface's Lock Statement: A Developer's Guide
Working with database concurrency can be tricky, especially when dealing with multi-user environments. Today, I'll walk you through Uniface'slock
statement, a powerful tool for managing database occurrence locking.This article was created with the assistance of AI and is based on the Uniface Documentation 10.4.
🎯 What is the Lock Statement?
Thelock
statement in Uniface locks the database occurrence of the current occurrence, ensuring that only the current process can modify it. This is crucial for maintaining data integrity in concurrent environments.
📊 Return Values and Status Codes
Understanding the return values is essential for proper error handling. Here's what you need to know:
💚 Success and Common Status Values
Status Value | Meaning |
---|---|
0 | ✅ Success - Occurrence is locked and can only be modified by the current process |
-1 | ❌ No active occurrence or no entities painted on the component |
-2 | 🔍 Occurrence not found - table is empty or occurrence removed since last retrieve |
-3 | 🎯 Hit for the occurrence does not exist |
-5 | 🔒 No hit for occurrence or occurrence is read-only (cannot be locked) |
-10 | ⚠️ Occurrence modified/removed since retrieved - should be reloaded |
-11 | 🔐 Occurrence already locked |
🚨 Error Handling with $procerror
The$procerror
variable provides additional error information:
- -1 (UGENERR_ERROR): General error occurred
- -2 (UIOSERR_OCC_NOT_FOUND): Occurrence or record not found
- -2 through -12 (UIOSERR_*): Database I/O errors
- -16 through -30 (UNETERR_*): Network I/O errors
💡 Practical Example
Here's a practical example of how to use thelock
statement with proper error handling:
trigger lock lock if ($status = -10) reload endifend; lock
This example demonstrates a common pattern: attempting to lock a record and reloading it if it has been modified since the last retrieval.
🔧 Important Considerations
🎭 DBMS Support
Keep in mind that if your DBMS doesn't support database locking or uses optimistic locking, thelock
statement will be ignored. This makes it safe to use across different database systems.
📋 Component Compatibility
Thelock
statement is allowed in all component types, making it versatile for various application scenarios.
🎯 Best Practices
- Always check the return status 📊 - Don't assume the lock was successful
- Handle the -10 status code 🔄 - Reload data when it has been modified
- Consider your DBMS 🗃️ - Understand how your database handles locking
- Test concurrent scenarios 👥 - Verify behavior with multiple users
🎉 Conclusion
The Unifacelock
statement is a powerful tool for managing database concurrency. By understanding its return values and implementing proper error handling, you can build robust applications that handle multi-user scenarios gracefully.
Remember to always test your locking logic in environments that simulate real-world concurrent access patterns!
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse