douchuitang0331 2016-10-16 23:24
浏览 94
已采纳

使用Golang预备语句的原始SQL事务

I'm having trouble finding some examples that do three of the following things:

1) Allow raw sql transactions in golang.

2) Use prepared statements.

3) Rollback on query failures.

I would like to do something like this, but with prepared statements.

    stmt, stmt_err := db.Prepare(`
            BEGIN TRANSACTION;

            -- Insert record into first table.

            INSERT INTO table_1 (
                    thing_1,
                    whatever)
            VALUES($1,$2);

            -- Inert record into second table.

            INSERT INTO table_2 (
                    thing_2,
                    whatever)
            VALUES($3,$4);

            END TRANSACTION;
            `)
    if stmt_err != nil {
            return stmt_err
    }   
    res, res_err := stmt.Exec(
            thing_1,
            whatever,
            thing_2,
            whatever)

When I run this, I get this error: pq: cannot insert multiple commands into a prepared statement

What gives? Are ACID compliant transactions even possible in golang? I cannot find an example.

EDIT no examples here.

  • 写回答

2条回答 默认 最新

  • douzhe9075 2016-10-17 00:18
    关注

    Yes Go has a great implementation of sql transactions. We start the transaction with db.Begin and we can end it with tx.Commit if everything goes good or with tx.Rollback in case of error.

    type Tx struct { }

    Tx is an in-progress database transaction.

    A transaction must end with a call to Commit or Rollback.

    After a call to Commit or Rollback, all operations on the transaction fail with ErrTxDone.

    The statements prepared for a transaction by calling the transaction's Prepare or Stmt methods are closed by the call to Commit or Rollback.

    Also note that we prepare queries with the transaction variable tx.Prepare(...)

    Your function may looks like this:

    func doubleInsert(db *sql.DB) error {
    
        tx, err := db.Begin()
        if err != nil {
            return err
        }
    
        {
            stmt, err := tx.Prepare(`INSERT INTO table_1 (thing_1, whatever)
                         VALUES($1,$2);`)
            if err != nil {
                tx.Rollback()
                return err
            }
            defer stmt.Close()
    
            if _, err := stmt.Exec(thing_1, whatever); err != nil {
                tx.Rollback() // return an error too, we may want to wrap them
                return err
            }
        }
    
        {
            stmt, err := tx.Prepare(`INSERT INTO table_2 (thing_2, whatever)
                         VALUES($1, $2);`)
            if err != nil {
                tx.Rollback()
                return err
            }
            defer stmt.Close()
    
            if _, err := stmt.Exec(thing_2, whatever); err != nil {
                tx.Rollback() // return an error too, we may want to wrap them
                return err
            }
        }
    
        return tx.Commit()
    }
    

    I have a full example here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab