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

Commit() function can not distinguish whether the transaction has been sent #1226

Open
syuanyuan708 opened this issue Jun 21, 2021 · 6 comments

Comments

@syuanyuan708
Copy link

syuanyuan708 commented Jun 21, 2021

Issue description

when I call func (tx *mysqlTx) Commit() (err error) ,if connection has problem it return an ErrInvalidConn

commit

but if current connection is fine it call exec

exec

and in exec it call readResultSetHeaderPacket after the transaction sent to db successfully, and readResultSetHeaderPacket call readPacket() and it also may lead to ErrInvalidConn

readpackets

thus I can't distinguish whether the transaction has been sent, it lead to one transaction sent twice and data not correct. Is it possible to add a new error to distinguish this two cases?

Example code

If possible, please enter some example code here to reproduce the issue.

err = tx.Commit(ctx)
if err != nil {
xlog.Errorf(ctx, "%s commit transaction failed, table:%v err:%v", fun, table, err) ...

Error log

***err:invalid connection 

Configuration

Driver version (or git SHA):

*Go version: go1.12 linux/amd64

*Server version: tidb

*Server OS: redhat7.2

@syuanyuan708
Copy link
Author

it happend in my project when db connection is instable and an item in db is updated twice, does anyone encountered similar situations please?

@zihengCat
Copy link
Contributor

Use newer go version (Go 1.13+) and try again.

@zihengCat
Copy link
Contributor

zihengCat commented Jul 14, 2021

@syuanyuan708
It's no need to distinguish error types. Driver handles error internally and user does not care about it.
When error occurred in a transaction, call Rollback() and return.

	err := tx.Commit()
	if err != nil {
		tx.Rollback()
		return
	}

@syuanyuan708
Copy link
Author

@zihengCat
ok, I will try to update Go 1.13+ and test it, thanks for your explain.

@Weltloose
Copy link

Weltloose commented Dec 26, 2021

@zihengCat After reading the code and debugging, I found that after successfully sending "Commit" message to server and failing in receiving response from server(e.g. io timeout), tx.Commit() returns "invalid connection" error, and then tx.Rollback() just do nothing for tx.done==1. So is it possible that commit has been executed in server but indicates a rollback to users?

@methane
Copy link
Member

methane commented Dec 31, 2021

I think database/sql should provide explicit guideline for this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants