DEV Community

Peter + AI
Peter + AI

Posted on

πŸ”„ Mastering Database Transactions in Uniface: The commit Statement Explained

As a developer working with Uniface, understanding database transaction management is crucial for building robust applications. Today, let's dive deep into one of the most important statements in Uniface: the commit statement! πŸ’Ύ

This article is based on the official Uniface 10.4 documentation and was created with the assistance of AI to help fellow developers understand this essential concept.

🎯 What is the commit Statement?

The commit statement in Uniface is your go-to tool for permanently saving database changes. It commits a transaction to a DBMS or specific path, ensuring your data modifications are safely stored.

Basic Syntax:

commit {"PathString"}
Enter fullscreen mode Exit fullscreen mode

Quick Example:

commit "DEF"
Enter fullscreen mode Exit fullscreen mode

πŸ“‹ Parameters Breakdown

The PathString parameter is where the magic happens! Here's how it works:

πŸ” Path with Dollar Sign ($)

When your PathString starts with a dollar sign, Uniface treats it as a path name:

commit "$DEF"
Enter fullscreen mode Exit fullscreen mode

This commits modifications to all entities accessed through that specific path, based on assignments.

πŸ—ƒοΈ DBMS Name (No Dollar Sign)

Without a dollar sign, the argument represents a DBMS defined in your modeled entities:

commit "DEF"
Enter fullscreen mode Exit fullscreen mode

This commits all entities assigned to that DBMS, determined by the modeled entity definition.

🌐 Global Commit (No Parameters)

Omit the parameter for a complete commit:

commit
Enter fullscreen mode Exit fullscreen mode

This commits all pending updates across all databases and unlocks all locked occurrences! πŸ”“

πŸ“Š Return Values & Error Handling

The commit statement returns values in $status to help you handle different scenarios:

βœ… Success Values

  • 0: Data successfully committed! πŸŽ‰

⚠️ Error Values

  • -3: Exceptional I/O error (hardware/software issues)
  • -9: Maximum DBMS logons reached
  • -16: Network error

Additional error constants in $procerror include:

  • -2 through -12: Database I/O errors (UIOSERR_*)
  • -16 through -30: Network I/O errors (UNETERR_*)
  • -1107: Invalid path name (UPROCERR_PATH)

πŸ’‘ Best Practices & Tips

πŸ—οΈ Component Level Strategy

Always place your commit (or rollback) statements at the highest component level to avoid currency problems:

If Component B is activated by Component A, place the commit statement in Component A, not Component B!

🧠 Memory Management Exception

If your component's Component Behavior property is set to "Keep component definition and data in memory", use commit at that component's level instead.

πŸ”¨ Practical Example

Here's a real-world example showing proper error handling with commit:

trigger store
 call CENSTORE
 return ($status)
end; store

function CENSTORE
store
if ($status < 0)
 message "Store error!"
 rollback
else
 message "Store complete."
 commit
 if ($status < 0)
 message "Commit error, rollback performed."
 rollback
 endif
endif
end; CENSTORE
Enter fullscreen mode Exit fullscreen mode

🎭 DEF vs $DEF: A Tale of Two Commits

Understanding the difference between commit "DEF" and commit "$DEF" is crucial:

Setup Scenario:

$DEF = $ORA
FRODO.MYMODEL = $SYB
Enter fullscreen mode Exit fullscreen mode

Different Behaviors:

  • commit "DEF" β†’ Commits all entities in MYMODEL (default DBMS)
  • commit "$DEF" β†’ Commits all entities except FRODO (path-based)

🎯 Key Takeaways

  • ✨ Use commit to permanently save your database changes
  • πŸ”„ Always handle return values for robust error management
  • πŸ“ Place commits at the appropriate component level
  • πŸ’° Understand the difference between path-based and DBMS-based commits
  • πŸ›‘οΈ Some DBMS don't support locking - commits are ignored in these cases

Understanding the commit statement is essential for any serious Uniface developer. It's the foundation of reliable data persistence and transaction management! πŸš€

Happy coding, and may your transactions always commit successfully! πŸ’ͺ

Top comments (0)