首页 > 解决方案 > 如何在多 goroutines 中运行 postgres Tx

问题描述

我必须在 groutine 中运行 tx.exec(),但它总是返回pq: bad conenction并且pq: unexpected Parse response 'D'

这是我的代码示例:

func (uc OdooUseCase) SynchronizeTourPackage(tourPackageID, partnerID string, odooPackageID int64) (err error) {
    var wg sync.WaitGroup

    odooUc := OdooUseCase{UcContract: uc.UcContract}
    odooDB, err := odooUc.BuildConnection(partnerID)
    if err != nil {
        logruslogger.Log(logruslogger.WarnLevel, err.Error(), functioncaller.PrintFuncName(), "odoo-build-connection")
        return err
    }
    defer odooDB.Close()

    tourPackagePriceUseCase := TourPackagePriceUseCase{UcContract: uc.UcContract, odooDB: odooDB}
    tourPackageMealUseCase := TourPackageMealUseCase{UcContract: uc.UcContract, odooDB: odooDB}

    errs := make(chan error)

    wg.Add(1)
    go func() {
        wg.Wait()
        close(errs)
    }()

    wg.Add(1)
    go func() {
        err := tourPackagePriceUseCase.SaveFromOdoo(tourPackageID, partnerID, odooPackageID)
        if err != nil {
            logruslogger.Log(logruslogger.WarnLevel, err.Error(), functioncaller.PrintFuncName(), "save-tour-package-price", uc.ReqID)
            errs <- err
        }
        defer wg.Done()
    }()

    wg.Add(1)
    go func() {
        err := tourPackageMealUseCase.SaveFromOdoo(tourPackageID, partnerID, odooPackageID)
        if err != nil {
            logruslogger.Log(logruslogger.WarnLevel, err.Error(), functioncaller.PrintFuncName(), "save-tour-package-meal", uc.ReqID)
            errs <- err
        }
        defer wg.Done()
    }()

    err = <-errs

    return err
}

我需要在 go 例程中运行一个 tx.exec,因为这个函数是从另一个数据库同步的。请问有人可以帮助我吗?谢谢

标签: postgresqlgogoroutinetx

解决方案


推荐阅读