在一个事务中在golang中执行多个查询的惯用方法

时间:2015-01-02 11:09:53

标签: go

我目前正在努力(第二天)找到进行多项查询的最佳方式,并且想知道你是否知道解决方案。

我有一个开放的* sql.DB连接,名为 myDb 并使用 go-sql-driver

func TruncateGalleryImport() error {

    s := make([]string, 0)

    s = append(s, "TRUNCATE TABLE add_map")
    s = append(s, "TRUNCATE TABLE album")
    s = append(s, "TRUNCATE TABLE album_permission")
    s = append(s, "TRUNCATE TABLE album_view")
    s = append(s, "TRUNCATE TABLE album_watch")
    s = append(s, "TRUNCATE TABLE media")
    s = append(s, "TRUNCATE TABLE media_user_view")
    s = append(s, "TRUNCATE TABLE media_view")
    s = append(s, "TRUNCATE TABLE media_watch")
    s = append(s, "TRUNCATE TABLE private_map")
    s = append(s, "TRUNCATE TABLE attachment")
    s = append(s, "TRUNCATE TABLE attachment_data")

    for _, q := range s {
        _, err := myDb.Exec(q)
        if err != nil {
            return err
        }
    }

    return nil
}

是否可以仅使用一个事务将所有上述查询一起提交?

干杯

2 个答案:

答案 0 :(得分:3)

使用事务,如下所示(请参阅代码中的注释):

func TruncateGalleryImport() error {
    s := make([]string, 0)

    s = append(s, "TRUNCATE TABLE add_map")
    s = append(s, "TRUNCATE TABLE album")
    s = append(s, "TRUNCATE TABLE album_permission")
    s = append(s, "TRUNCATE TABLE album_view")
    s = append(s, "TRUNCATE TABLE album_watch")
    s = append(s, "TRUNCATE TABLE media")
    s = append(s, "TRUNCATE TABLE media_user_view")
    s = append(s, "TRUNCATE TABLE media_view")
    s = append(s, "TRUNCATE TABLE media_watch")
    s = append(s, "TRUNCATE TABLE private_map")
    s = append(s, "TRUNCATE TABLE attachment")
    s = append(s, "TRUNCATE TABLE attachment_data")

    // Get new Transaction. See http://golang.org/pkg/database/sql/#DB.Begin
    txn, err := myDb.Begin()

    if err != nil {
        return err
    }

    defer func() {
        // Rollback the transaction after the function returns.
        // If the transaction was already commited, this will do nothing.
        _ = txn.Rollback()
    }()

    for _, q := range s {
        // Execute the query in the transaction.
        _, err := txn.Exec(q)

        if err != nil {
            return err
        }
    }

    // Commit the transaction.
    return txn.Commit()
}

答案 1 :(得分:2)

您可以使用包装函数来执行提交/回滚逻辑,甚至可以使用字符串匹配扩展错误处理。

// RDBTransaction is a function which abstracts a sql transaction
// into a function with an isolation level (isolvl) parameter.
// the following integers represent the available isolation levels (isolvl)
//  1: SERIALIZABLE
//  2: REPEATABLE READ
//  3: READ COMMITTED
//  4: READ UNCOMMITTED
func RDBTransaction(db *sql.DB, isolvl int, fn func(*sql.Tx) error) (err error) {
    var tx *sql.Tx
    tx, err = db.Begin()
    if err != nil {
        return err
    }

    // transaction isolation level setting
    switch isolvl {
    case 1:
        _, err = tx.Exec(`set transaction isolation level serializable`)
    case 2:
        _, err = tx.Exec(`set transaction isolation level repeatable read`)
    case 3:
        _, err = tx.Exec(`set transaction isolation level read committed`)
    case 4:
        _, err = tx.Exec(`set transaction isolation level read uncommitted`)
    default:
        _, err = tx.Exec(`set transaction isolation level serializable`)
    }
    if err != nil {
        return err
    }

    // catch all, commit/rollback
    defer func() {
        if err != nil {
            tx.Rollback()
            return
        }
        err = tx.Commit()
    }()

    // run transaction
    err = fn(tx)

    return err
}