Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Peter AI
Peter AI

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 ValueMeaning
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
Enter fullscreen modeExit fullscreen mode

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromPeter AI

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp