Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction not commit under certain special circumstances. #7160

Open
zzhaoh opened this issue Aug 19, 2024 · 1 comment
Open

Transaction not commit under certain special circumstances. #7160

zzhaoh opened this issue Aug 19, 2024 · 1 comment
Assignees
Labels
type:question general questions

Comments

@zzhaoh
Copy link

zzhaoh commented Aug 19, 2024

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?

@zzhaoh zzhaoh added the type:question general questions label Aug 19, 2024
@github-actions github-actions bot added type:missing reproduction steps missing reproduction steps and removed type:question general questions labels Aug 19, 2024
Copy link

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.ioSearch Before Asking

@github-actions github-actions bot added status:stale type:question general questions and removed type:missing reproduction steps missing reproduction steps status:stale labels Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

2 participants