【发布时间】:2017-02-25 20:37:27
【问题描述】:
我很难找到一些执行以下三项操作的示例:
1) 在 golang 中允许原始 sql 事务。
2) 使用准备好的语句。
3) 查询失败时回滚。
我想做这样的事情,但有准备好的陈述。
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)
当我运行它时,我得到这个错误:
pq: cannot insert multiple commands into a prepared statement
什么给了?在 golang 中是否可以进行符合 ACID 的事务?我找不到例子。
编辑 没有例子here。
【问题讨论】:
标签: postgresql go transactions acid