Skip to content

Document TransactionalEnforcer for atomic policy and business data updates#1703

Open
Copilot wants to merge 4 commits intomasterfrom
copilot/ensure-transaction-consistency
Open

Document TransactionalEnforcer for atomic policy and business data updates#1703
Copilot wants to merge 4 commits intomasterfrom
copilot/ensure-transaction-consistency

Conversation

Copy link
Contributor

Copilot AI commented Feb 1, 2026

Casbin's TransactionalEnforcer provides atomic coordination between policy operations and business database transactions, but was undocumented. Users deploying RBAC systems need to update user roles in their database and Casbin policies atomically to avoid inconsistency.

Changes

  • TRANSACTION_GUIDE.md - Comprehensive documentation covering:

    • WithTransaction pattern for automatic commit/rollback
    • Two-phase commit protocol with conflict detection
    • TransactionalAdapter interface implementation
    • GORM adapter integration example
  • examples/transaction/ - Working examples demonstrating:

    • Basic transactions with automatic rollback
    • User role management pattern
    • Batch policy operations
  • README.md - Added transaction support section with usage snippet

Usage

enforcer, _ := casbin.NewTransactionalEnforcer("model.conf", transactionalAdapter)

err := enforcer.WithTransaction(ctx, func(tx *casbin.Transaction) error {
    // Database and Casbin operations in same transaction
    db.UpdateUserRole(userId, "admin")
    tx.AddGroupingPolicy(userId, "admin")
    return nil // Both commit atomically
})

Adapters must implement persist.TransactionalAdapter interface. Guide includes complete GORM implementation example.

Original prompt

This section details on the original issue you should resolve

<issue_title>How to achieve transaction consistency between Casbin policy operations and other database operations?</issue_title>
<issue_description>Description
In our application, we need to update both business data and Casbin authorization policies atomically. For example, when changing a user's role:

Update the user's role in the business database

Update the corresponding Casbin grouping policy

Currently, these operations happen in separate transactions, risking data inconsistency if one succeeds and the other fails.

Questions:

What are the recommended patterns for ensuring transaction consistency between Casbin policies and business data?

Does Casbin provide built-in support for coordinating these operations within a single transaction?

Are there adapter-specific solutions (e.g., for Gorm, Xorm, or specific databases)?

Context:

We're using gorm-adapter

Database: PostgreSQL

Application framework: [specify framework, e.g., Go Gin, Spring Boot]

Any guidance or examples would be greatly appreciated!</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI and others added 3 commits February 1, 2026 14:58
Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
… comments

Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
Copilot AI changed the title [WIP] Update transaction handling for Casbin policy operations Document TransactionalEnforcer for atomic policy and business data updates Feb 1, 2026
Copilot AI requested a review from nomeguy February 1, 2026 15:02
@nomeguy nomeguy marked this pull request as ready for review February 1, 2026 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to achieve transaction consistency between Casbin policy operations and other database operations?

3 participants