gorm icon indicating copy to clipboard operation
gorm copied to clipboard

use callbacks.Begin as a hook to handle begin operation

Open sunfuze opened this issue 2 years ago • 2 comments

make transaction support hooks

  • [x] Do only one thing
  • [x] Non breaking API changes
  • [x] Tested

What did this pull request do?

Use callbacks.Begin as hook to handle real transaction operation, make transaction support before/after hooks.

User Case Description

  • Database sharding: shard database by info in context
  • Implement distributed transaction

sunfuze avatar Jul 06 '22 08:07 sunfuze

notes to myself:

The Transaction hooks API will be behaves like:

tx := db.Scopes(OnTransaction{
       OnCommit: func(tx *gorm.TransactionInterface) {
               // xxx
               tx.Commit()
               // xxx
       },
       OnRollback: func(tx *gorm.TransactionInterface) {
               // xxx
               tx.Rollback()
               // xxx
       },
       OnBegin: func(db *gorm.DBInterface) {
               // xxx
               tx.Begin()
               // xxx
       },
}).Begin()

tx.Commit()

jinzhu avatar Jul 14 '22 12:07 jinzhu