本地事务

概述

sqlx.SqlConn 提供了基础的事务机制,简单实例:

  1. var conn sqlx.SqlConn
  2. err := conn.TransactCtx(context.Background(), func(ctx context.Context, session sqlx.Session) error {
  3. r, err := session.ExecCtx(ctx, "insert into user (id, name) values (?, ?)", 1, "test")
  4. if err != nil {
  5. return err
  6. }
  7. r ,err =session.ExecCtx(ctx, "insert into user (id, name) values (?, ?)", 2, "test")
  8. if err != nil {
  9. return err
  10. }
  11. })