doutang7707 2017-10-07 19:01
浏览 40

如何在Go中处理事务和存储过程

I want to call some stored procedure from Go that has transactions to make the execution atomic.

I read this article here: http://go-database-sql.org/modifying.html

You should not mingle the use of transaction-related functions such as Begin() and Commit() with SQL statements such as BEGIN and COMMIT in your SQL code. Bad things might result

It says that I should avoid using BEGIN / COMMIT, and I also assume START TRANSACTION in my SQL if I use Begin() and Commit().

What if I made a stored procedure that contained START TRANSACTION and COMMIT.

Could I just skip the Begin() and Commit() functions in Go, and just do an Exec() like this?

query := `CALL comment_branch_delete(?, ?);`
if _, err := mysql.DB.Exec(query, commentId, postId); err != nil {
    return err
}

Would this be atomic? If the comment_branch_delete stored procedure contains START TRANSACTION AND COMMIT?

Or should I use Tx variable?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?