Connections shutdown error when using prepared statement

115 Views Asked by At

I'm trying to solve a problem with a method in microservice that runs twice a day (7 p.m. and 7 a.m.). The microservice has 27 connections, all of them to Firebird, and when the error occurs I need to restart the method manually (it works fine on restart because the package that we use reconnects when the connection is not valid). The problem occurs time to time.

Please take a look at the code below. We use SQLX, Firebird version 3.0.

// The func is called inside a handler and len(stuff) is always > 1000.
func SetStuff(ctx context.Context, stuff []model.Stuff) (err error) {
    for _, v := range stuff {
        tx, err := db.BeginTxx(ctx, nil)
        if err != nil {
            return err
        } 
        defer func() {
            errRollback := tx.Rollback()
            if err != nil {
                if errRollback != sql.ErrTxDone {
                    err = fmt.Errorf("unable to rollback transaction: %w", errRollback, err)
                    return
                }
            }
        }()
        _, err = tx.ExecContext(ctx, "SOME UPDATE", v)
        if err != nil {
            return 
        }
    
        err = tx.Commit()
        return
    }
    return
}

I've tried to reproduce the same error, but it wasn't successful.

Error in logs:

unable to rollback transaction: read tcp **********: read: connection reset by peer
0

There are 0 best solutions below