You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@casbin.apache.org by "official-burak (via GitHub)" <gi...@apache.org> on 2026/08/27 14:39:16 UTC

Re: [I] How to achieve transaction consistency between Casbin policy operations and other database operations? [casbin]

official-burak commented on issue #1702:
URL: https://github.com/apache/casbin/issues/1702#issuecomment-5440737150

   Casbin does not wrap your business tables. gorm-adapter can share the same GORM transaction as the role update.
   
   Call `TurnOffAutoMigrate` once on the process db, then hand that `tx` to the adapter:
   
   ```go
   gormadapter.TurnOffAutoMigrate(db)
   
   err := db.Transaction(func(tx *gorm.DB) error {
       if err := tx.Model(&User{}).Where("id = ?", id).Update("role", role).Error; err != nil {
           return err
       }
       a, err := gormadapter.NewAdapterByDB(tx)
       if err != nil {
           return err
       }
       orig := e.GetAdapter()
       e.SetAdapter(a)
       defer e.SetAdapter(orig)
       _, err = e.AddGroupingPolicy(user, role)
       return err
   })
   ```
   
   Keep AutoSave on and use `AddGroupingPolicy` / `RemoveGroupingPolicy`. `SavePolicy` rewrites the whole `casbin_rule` table.
   
   `Adapter.Transaction` and `NewTransactionalEnforcer` + `WithTransaction` only cover policy rows. They do not include the user row unless you pass the same `tx` as above.


-- 
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: commits-unsubscribe@casbin.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org
For additional commands, e-mail: commits-help@casbin.apache.org