All skills
gogf avatar

/goframe-v2

@911705d
by GoFramegogf/skills81 stars
3

GoFrame v2 development skill. Use only when the target Go project uses or is explicitly adopting GoFrame v2: the nearest go.mod requires github.com/gogf/gf/v2, existing Go files import github.com/gogf/gf/v2 or any github.com/gogf/gf/v2/... component package, or the user asks to scaffold, migrate, or build with GoFrame. Trigger for GoFrame-backed Go work such as APIs/controllers/services, middleware, routing/config, ORM/DAO/DO/entity/database operations, gf CLI/codegen, HTTP/gRPC services, and microservice conventions. Do not trigger for generic Go projects without GoFrame evidence, frontend-only work, shell scripts, or unrelated infrastructure tasks.

Use this Skill: https://skilld.dev/gh/gogf/skills/goframe-v2

This session only. Nothing lands on disk.

references核心组件数据库ORMORM事务处理ORM事务处理-隔离级别.md

事务隔离级别是数据库事务处理的重要特性,它定义了一个事务可能受其他并发事务影响的程度。GoFrame ORM组件支持标准的SQL事务隔离级别,使开发者能够根据应用需求选择适当的隔离级别。

基本概念

事务隔离级别主要解决并发事务执行时可能出现的以下问题:

  1. 脏读(Dirty Read):一个事务读取了另一个未提交事务修改过的数据。
  2. 不可重复读(Non-repeatable Read):在同一事务内,多次读取同一数据返回的结果有所不同。
  3. 幻读(Phantom Read):在同一事务内,多次执行同一查询返回不同的数据集(行数变化)。

隔离级别类型

GoFrame ORM 支持以下标准的事务隔离级别:

隔离级别 脏读 不可重复读 幻读 说明
读未提交(Read Uncommitted) 可能 可能 可能 最低的隔离级别,允许读取未提交的数据变更
读已提交(Read Committed) 不可能 可能 可能 只能读取已经提交的数据
可重复读(Repeatable Read) 不可能 不可能 可能 MySQL 的默认级别,确保同一事务中多次读取数据一致
串行化(Serializable) 不可能 不可能 不可能 最高的隔离级别,完全串行执行

优势与应用场景

不同的隔离级别适用于不同的应用场景:

读未提交(Read Uncommitted)

优势:

  • 性能最好,没有锁开销
  • 并发度最高

应用场景:

  • 对数据一致性要求极低的场景
  • 只读统计分析,允许少量不精确数据
  • 性能要求极高且能容忍数据不一致的场景

读已提交(Read Committed)

优势:

  • 避免脏读问题
  • 较好的并发性能
  • 大多数数据库的默认级别

应用场景:

  • 需要避免脏读但可以接受不可重复读的场景
  • 读多写少的应用
  • 对数据一致性有一定要求但也关注性能的场景

可重复读(Repeatable Read)

优势:

  • 避免脏读和不可重复读问题
  • MySQL 的默认隔离级别
  • 在同一事务中保证数据读取一致性

应用场景:

  • 需要在事务内多次读取并保证数据一致的场景
  • 财务类应用
  • 对数据一致性要求较高的业务逻辑

串行化(Serializable)

优势:

  • 完全避免并发问题
  • 提供最高级别的数据一致性保证

应用场景:

  • 对数据一致性要求极高的关键业务
  • 银行转账等金融核心交易
  • 数据完整性比性能更重要的场景

在 GoFrame 中使用事务隔离级别

GoFrame ORM 通过 TxOptions 结构体支持设置事务隔离级别。以下是使用不同隔离级别的示例:

基本用法

// 使用特定隔离级别开始事务
tx, err := db.BeginWithOptions(ctx, gdb.TxOptions{
    Isolation: sql.LevelReadCommitted,
})
if err != nil {
    // 处理错误
    return
}
// 确保事务最终会提交或回滚
defer func() {
    if err := recover(); err != nil {
        tx.Rollback()
        panic(err)
    }
}()

// 执行事务操作
_, err = tx.Insert(ctx, "user", g.Map{"name": "john"})
// SQL: INSERT INTO user(name) VALUES('john')

// 提交事务
if err = tx.Commit(); err != nil {
    // 处理错误
    tx.Rollback()
    return
}

使用不同隔离级别的示例

读已提交(Read Committed)
// 使用读已提交隔离级别
tx, err := db.BeginWithOptions(ctx, gdb.TxOptions{
    Isolation: sql.LevelReadCommitted,
})
if err != nil {
    return
}
defer tx.Rollback()

// 查询用户余额
balance, err := tx.Model("account").Where("user_id", 1).Value("balance")
// SQL: SELECT balance FROM account WHERE user_id=1

// 更新余额
_, err = tx.Model("account").Where("user_id", 1).Update(g.Map{"balance": balance.Int() + 100})
// SQL: UPDATE account SET balance=balance+100 WHERE user_id=1

if err = tx.Commit(); err != nil {
    return
}
可重复读(Repeatable Read)
// 使用可重复读隔离级别(MySQL默认)
tx, err := db.BeginWithOptions(ctx, gdb.TxOptions{
    Isolation: sql.LevelRepeatableRead,
})
if err != nil {
    return
}
defer tx.Rollback()

// 第一次查询用户数据
user1, err := tx.Model("user").Where("id", 1).One()
// SQL: SELECT * FROM user WHERE id=1

// ... 其他操作

// 第二次查询相同用户数据,在可重复读级别下,即使其他事务修改了该数据,这里读取的结果仍与第一次相同
user2, err := tx.Model("user").Where("id", 1).One()
// SQL: SELECT * FROM user WHERE id=1

if err = tx.Commit(); err != nil {
    return
}
串行化(Serializable)
// 使用串行化隔离级别
tx, err := db.BeginWithOptions(ctx, gdb.TxOptions{
    Isolation: sql.LevelSerializable,
})
if err != nil {
    return
}
defer tx.Rollback()

// 查询满足条件的所有用户
users, err := tx.Model("user").Where("status", "active").All()
// SQL: SELECT * FROM user WHERE status='active'

// 在串行化级别下,其他事务无法同时修改或添加符合此条件的记录
// 这确保了在事务执行期间,查询结果的一致性

if err = tx.Commit(); err != nil {
    return
}

使用事务闭包函数

GoFrame ORM还提供了便捷的事务闭包函数,可以同时指定隔离级别:

err := db.TransactionWithOptions(ctx, gdb.TxOptions{
    Isolation: sql.LevelSerializable,
}, func(ctx context.Context, tx gdb.TX) error {
    // 执行事务操作
    _, err := tx.Insert(ctx, "user", g.Map{"name": "john"})
    // SQL: INSERT INTO user(name) VALUES('john')
    
    if err != nil {
        // 返回错误会自动回滚事务
        return err 
    }
    
    // 返回nil会自动提交事务
    return nil
})

注意事项

  1. 隔离级别越高,并发性能越低,请根据业务需求选择合适的隔离级别。
  2. 不同数据库对隔离级别的实现可能有所不同,请参考具体数据库的文档。
  3. MySQL的默认隔离级别是“可重复读”(Repeatable Read)。
  4. 在使用高隔离级别时,需要注意死锁的可能性。
  5. 如果不指定隔离级别,GoFrame ORM将使用数据库的默认隔离级别。

实际应用示例

电商订单处理

// 使用可重复读隔离级别处理订单
err := db.TransactionWithOptions(ctx, gdb.TxOptions{
    Isolation: sql.LevelRepeatableRead,
}, func(ctx context.Context, tx gdb.TX) error {
    // 1. 查询商品库存
    stock, err := tx.Model("product").Where("id", productId).Value("stock")
    // SQL: SELECT stock FROM product WHERE id=?
    
    if err != nil {
        return err
    }
    
    // 2. 检查库存是否充足
    if stock.Int() < quantity {
        return errors.New("库存不足")
    }
    
    // 3. 创建订单
    orderId, err := tx.Model("order").InsertAndGetId(g.Map{
        "user_id": userId,
        "product_id": productId,
        "quantity": quantity,
        "status": "pending",
    })
    // SQL: INSERT INTO order(user_id,product_id,quantity,status) VALUES(?,?,?,'pending')
    
    if err != nil {
        return err
    }
    
    // 4. 减少库存
    _, err = tx.Model("product").Where("id", productId).
        Update(g.Map{"stock": gdb.Raw("stock - ?", quantity)})
    // SQL: UPDATE product SET stock=stock-? WHERE id=?
    
    return err
})

银行转账

// 使用串行化隔离级别处理转账
err := db.TransactionWithOptions(ctx, gdb.TxOptions{
    Isolation: sql.LevelSerializable,
}, func(ctx context.Context, tx gdb.TX) error {
    // 1. 检查源账户余额
    sourceBalance, err := tx.Model("account").Where("id", sourceAccountId).Value("balance")
    // SQL: SELECT balance FROM account WHERE id=?
    
    if err != nil {
        return err
    }
    
    if sourceBalance.Float64() < amount {
        return errors.New("余额不足")
    }
    
    // 2. 减少源账户余额
    _, err = tx.Model("account").Where("id", sourceAccountId).
        Update(g.Map{"balance": gdb.Raw("balance - ?", amount)})
    // SQL: UPDATE account SET balance=balance-? WHERE id=?
    
    if err != nil {
        return err
    }
    
    // 3. 增加目标账户余额
    _, err = tx.Model("account").Where("id", targetAccountId).
        Update(g.Map{"balance": gdb.Raw("balance + ?", amount)})
    // SQL: UPDATE account SET balance=balance+? WHERE id=?
    
    // 4. 记录交易历史
    _, err = tx.Model("transaction").Insert(g.Map{
        "source_account_id": sourceAccountId,
        "target_account_id": targetAccountId,
        "amount": amount,
        "time": gtime.Now(),
    })
    // SQL: INSERT INTO transaction(source_account_id,target_account_id,amount,time) VALUES(?,?,?,?)
    
    return err
})

总结

事务隔离级别是数据库并发控制的重要机制,GoFrame ORM 通过支持标准的SQL隔离级别,使开发者能够根据应用需求灵活选择合适的隔离级别,在数据一致性和性能之间取得平衡。

在实际应用中,建议根据业务场景的具体需求选择合适的隔离级别:

  • 对于一般的 Web 应用,“读已提交”通常是一个好的选择
  • 对于财务和订单处理,“可重复读”提供了更好的数据一致性保证
  • 对于极其重要的金融交易,可以考虑使用“串行化”,但需要注意性能影响

Source: SKILL.md on GitHub

1 alert16d5 checks · Risk MEDIUM
  • Gen Agent Trust Hub16d

    The skill provides a comprehensive development guide and code examples for the GoFrame v2 framework. While highly informative for Go developers, it contains several examples with hardcoded placeholder secrets, tokens, and specific local file paths to sensitive configuration files (like Kubernetes credentials). These elements are provided for demonstration purposes but represent insecure practices if used in production without modification.

  • Socket16d

    5 alerts: gptSecurity, gptAnomaly

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    227/949 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 911705d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 months ago.

Steadyupdated 4 months ago
  • Go
  • Database
  • goframe
  • orm
  • http
  • grpc
  • microservices
  • crud
  • scaffolding
  • middleware

README badge

README badge for gogf/skills

Provides instruction and conventions for developing with GoFrame v2, a modular Go framework for building HTTP and gRPC services, microservices, and database operations using its ORM, CLI tooling, and project scaffolding. Use this skill when the target project has GoFrame v2 as a dependency or when the user requests scaffolding, migrations, or service development with GoFrame conventions.

Generated from the current SKILL.md.

When should I use this skill?
Use this skill only when your Go project uses GoFrame v2, indicated by github.com/gogf/gf/v2 in go.mod or imports. Do not use for generic Go projects, frontend work, or infrastructure tasks unrelated to GoFrame.
Should I manually set created_at and updated_at fields?
No. GoFrame automatically writes created_at on insert and updated_at on insert/update/save. Manually setting these fields is redundant and violates project conventions.
What should I use for database operations instead of g.Map?
Always use DO objects from internal/model/do/. DO struct fields are interface{}, unset fields remain nil and are automatically ignored by the ORM.
How does soft delete work in GoFrame?
Call Delete() on the DAO; GoFrame automatically converts it to UPDATE SET deleted_at = NOW(). Queries automatically filter out soft-deleted rows without requiring manual WhereNull conditions.
Where should I implement business logic?
Implement business logic directly in the service/ directory. Do not use the logic/ directory unless explicitly requested.

Generated from the current SKILL.md. These answers refresh after source changes.