Using the GoFrame ORM
component for transaction operations is very simple and safe, and can be achieved through two methods.
- Conventional Operation: After starting a transaction through
Begin
, it returns a transaction operation interfacegdb.TX
, which can then be used for operations and chain operations as introduced in previous sections. Conventional operations are prone to missing transaction closures, posing certain transaction safety risks. - Closure Operation: Transactions are handled in the form of closure methods using
Transaction
, where all transaction logic is implemented within the closure, and the transaction is automatically closed after the closure ends to ensure transaction safety. Additionally, closure operations support very convenient nested transactions, which are transparent and seamless in business operations.
We recommend all transaction operations be uniformly implemented using the Transaction
closure method.
Interface Documentation: https://pkg.go.dev/github.com/gogf/gf/v2/database/gdb#TX
📄️ ORM Transaction - Methods
Basic operations for ORM transaction handling in the GoFrame framework, including how to use the Begin, Commit, and Rollback methods to start, commit, and rollback transactions. It is particularly important to close transactions promptly after use to avoid resource leaks, and it is recommended to use the Transaction closure method for safe transaction operations.
📄️ ORM Transaction - Closure
The advantages of using closure operations for ORM transaction handling in the GoFrame framework, including reducing redundant code, lowering operational risks, and simplifying nested transactions, achieving automatic commit and rollback functions, and introducing the role of context parameters in nested transaction management to ensure transaction processing safety and simplicity.
📄️ ORM Transaction - Nested
Using the ORM functionality in the GoFrame framework to handle nested transactions. The article explains in detail the basic principles, methods, and log information of nested transactions, provides examples of regular operations and closure operations, and points out potential issues. Finally, a reference example of nested transactions in a project is provided to help developers understand how to apply this in real-world projects.