hsluoyz commented on issue #1706:
URL: https://github.com/apache/casbin/issues/1706#issuecomment-5206828369
@kuchaguangjie
You can now share one transaction between GORM and Casbin:
`Transaction.GetAdapter()` returns the adapter bound to the open transaction,
so your own SQL and the policy changes commit or roll back together. Fixed in
9a94fde4757836d1327ac8f8df0877ffc97ca66b.
```go
err := e.WithTransaction(ctx, func(tx *casbin.Transaction) error {
// Same tx as the policy changes below.
db :=
tx.GetAdapter().(*gormadapter.Adapter).GetDb().Session(&gorm.Session{NewDB:
true})
if err := db.Where("name = ?", "admin").Delete(&Role{}).Error; err !=
nil {
return err
}
_, err := tx.RemoveGroupingPolicy("alice", "admin")
return err
})
```
Note the `Session(&gorm.Session{NewDB: true})`: gorm-adapter pins its
`*gorm.DB` to the policy table, so without resetting the statement your queries
would run against `casbin_rule`. Works with the released gorm-adapter, no
adapter change needed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]