gorm
gorm copied to clipboard
use callbacks.Begin as a hook to handle begin operation
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
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()