Skip to content

casbin and gorm in the same tx #1706

@kuchaguangjie

Description

@kuchaguangjie

Casbin has it's own tx API. e.g:

// update roles, in tx
err = ef.WithTransaction(ctx, func(tx *casbin.Transaction) error {
	for _, rule := range addRules { // add
		_, err = tx.AddGroupingPolicy(deptCasbinKey, rule)
		if err != nil {
			return err
		}
		numPolicyUpdated++
	}
	for _, rule := range deleteRules { // delete
		_, err = tx.RemoveGroupingPolicy(deptCasbinKey, rule)
		if err != nil {
			return err
		}
		numPolicyUpdated++
	}

	return nil
})
if err != nil {
	return 0, apperr.WithStack(err)
}

gorm has it's own tx api too:

err := DB.Transaction(func(tx *gorm.DB) error {
	// ...
	return nil
})
if err != nil {
	lg.Error("test tx failed", zap.Error(err))
}

I have some operations involve both gorm and casbin operations, e.g delete a role and its permission from casbin;
Now I've to do it in 2 tx, 1 for gorm, 1 for casbin.
The 2 can't operate in the same tx, even casbin share the same connection pool with gorm.

Any idea how to make the whole operation in the same tx ?

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions