亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

與PGX在高浪的交易

與PGX在高浪的交易

Go
白衣染霜花 2022-10-04 17:13:14
我目前正在創建一個小的Go應用程序?,F在我正在研究DB部分。我使用的庫是這樣的:https://github.com/jackc/pgx我遇到的問題是,每次我嘗試執行數據庫讀取時,它都告訴我我的“conn很忙”。我讀到了有關使用pgxpool而不是單個連接的信息,但它仍然不起作用。我做錯了什么?func (postgre *PostgreClient) read(query string) (pgx.Row, error) {    client, err := postgre.client.Acquire(context.TODO())    transaction, err := client.BeginTx(context.TODO(), pgx.TxOptions{})    if err != nil {        return nil, err    }    defer transaction.Rollback(context.TODO())    rows := transaction.QueryRow(context.TODO(), query)    if err != nil {        return nil, err    }    err = transaction.Commit(context.TODO())    return rows, err}提前致謝。
查看完整描述

1 回答

?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

在提交事務之前,必須掃描該行。


如果您希望事務的處理保留在函數中,則可以傳遞一個接口,該接口也可以在函數內執行掃描。


例如:


// implemented by *sql.Row & *sql.Rows

type Row interface {

    Scan(dst ...interface{}) error

}


// implemented by your "models"

type RowScanner interface {

    ScanRow(r Row) error

}

type User struct {

    Id    int

    Email string

}


func (u *User) ScanRow(r Row) error {

    return r.Scan(

        &u.Id,

        &u.Email,

    )

}

func (postgre *PostgreClient) read(query string, rs RowScanner) (err error) {

    conn, err := postgre.client.Acquire(context.TODO())

    if err != nil {

        return err

    }

    defer conn.Release()

    

    tx, err := conn.BeginTx(context.TODO(), pgx.TxOptions{})

    if err != nil {

        return err

    }

    defer func() {

        if err != nil {

            tx.Rollback(context.TODO())

        } else {

            tx.Commit(context.TODO())

        }

    }()


    row := tx.QueryRow(context.TODO(), query)

    if err != nil {

        return nil, err

    }

    return rs.ScanRow(row) 

}

u := new(User)

if err := pg.read("select id, email from users limit 1", u); err != nil {

    panic(err)

}

要掃描模型列表:


type UserList []*User


func (ul *UserList) ScanRow(r Row) error {

    u := new(User)

    if err := u.ScanRow(r); err != nil {

        return err

    }


    *ul = append(*ul, u)

    return nil

}

func (postgre *PostgreClient) list(query string, rs RowScanner) (err error) {

    conn, err := postgre.client.Acquire(context.TODO())

    if err != nil {

        return err

    }

    defer conn.Release()

    

    tx, err := conn.BeginTx(context.TODO(), pgx.TxOptions{})

    if err != nil {

        return err

    }

    defer func() {

        if err != nil {

            tx.Rollback(context.TODO())

        } else {

            tx.Commit(context.TODO())

        }

    }()


    rows, err := tx.Query(context.TODO(), query)

    if err != nil {

        return err

    }

    defer rows.Close()

    

    for rows.Next() {

        if err := rs.ScanRow(rows); err != nil {

            return err

        }

    }

    return rows.Err()

}

ul := new(UserList)

if err := pg.list("select id, email from users", ul); err != nil {

    panic(err)

}


查看完整回答
反對 回復 2022-10-04
  • 1 回答
  • 0 關注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號