Your Question

version

  • MySQL 8.1.0
  • gorm.io/driver/mysql v1.5.1
  • gorm.io/gorm v1.25.2

Description

I ran into a really weird problem: at some point (it didn’t happen before), transactions in my production environment suddenly stopped committing properly and there were no error messages. It was like they were just hanging, which meant locks weren’t being released and other requests couldn’t be processed. But my testing environment (using MySQL 8.0) didn’t have this issue. Then I set up MySQL 8.1 in the testing environment and imported the relevant data from production, and strangely, everything worked fine in the testing environment. After that, I reinstalled MySQL 8.1 in production and migrated the data, and the problem went away.

Here is some of my code:

func ExecTransaction(f func(tx *gorm.DB) error) (err error) {
    start := time.Now()
    logger.Info("transaction start:" + start.Format("2006-01-02 15:04:05.000"))
        // global.Database is a initialized gorm.db
    err = global.Database.Transaction(f)
    end := time.Now()
    logger.Info(fmt.Sprintf("transaction end:%s,time cost:%fs",end.Format("2006-01-02 15:04:05.000"), end.Sub(start).Seconds()))
    return
}

func HandleRequest() (err error) {
    err = ExecTransaction(func(tx *gorm.DB) error {
        // insert into tableA
        if err := tx.Create(&TableA{}).Error; err != nil {
            return err
        }

        // update from tableB
        if err := tx.Updates(&TableA{}).Error; err != nil {
            return err
        }

                // delete from tableC
        err = tx.Delete(&TableC{}).Error
        if err != nil {
            return err
        }

        // insert into tableC
        if err := tx.Create(&TableC{}).Error; err != nil {
            return err
        }
        return nil
    })
    if err != nil {
        logger.Error("HandleRequest", err)
        return err
    }
    return nil
}

The document you expected this should be explained

Expected answer

Has anyone else run into something like this?

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: github-actions[bot]

This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days