go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.03k stars 3.86k forks source link

An error occurs in a multi-coroutine scenario #7066

Open exasasas opened 2 weeks ago

exasasas commented 2 weeks ago

I have started a program with many coroutines. In this case, when my gorm has been executed for a period of time, it returns an empty struct but does not return an error. This seems to happen randomly because I There are 2 pods running at the same time but always appear in one of them. no return ErrRecordNotFound or other err but return empty struct

This is my config

db, _ := gorm.Open(mysql.Open(dsn_master), &gorm.Config{Logger: logger.New(
        Writer{}, // io writer
        logger.Config{
            SlowThreshold: time.Second / 2, // Slow SQL threshold
            LogLevel:      logger.Info,     // Log level
        },
    )})
    db.Use(dbresolver.Register(dbresolver.Config{
        Sources:  []gorm.Dialector{mysql.Open(dsn_master)},
        Replicas: []gorm.Dialector{mysql.Open(dsn_slave)},
        Policy:   dbresolver.RandomPolicy{},
    }).
        SetConnMaxIdleTime(time.Hour).
        SetConnMaxLifetime(time.Hour).
        SetMaxIdleConns(500).
        SetMaxOpenConns(4000))

ivila commented 2 weeks ago

Please submit a runable example, or we are just wasting time here.

exasasas commented 2 weeks ago

Please submit a runable example, or we are just wasting time here.

It is a simple query. I have a go program that has opened many coroutines. These coroutines will sleep. The query is executed in the coroutines. After running for some time, the results are generated.

    err := db.Where("id = ?", id).First(&data).Error
    if err != nil {
        return nil, err
    }
ivila commented 2 weeks ago

Please submit a runable example, or we are just wasting time here.

A runable example contains huge information than what you provided:

  1. the go.mod: the version of GORM and the driver version you are using
  2. the codes: how your codes runs, did you accidently eat the errors?(for example, a gorm plugin)

It's very recommended that you should write one rather than just doing questions and answers, like I said, it's just wasting time here.