go-sql-driver / go-sql-driver/mysql

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

Open
#1,226 6 comments 0 reactions 0 assignees View on GitHub
database/sql issue
Dominant language
Go
Stars
15.3k
Forks
2.3k
Avg merge
2h 23m
Merged PRs (30d)
10

Description

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

![commit](https://user-images.githubusercontent.com/10654512/122767233-034a4c80-d2d5-11eb-8e90-90b70bb7481b.png)

but if current connection is fine it call exec

![exec](https://user-images.githubusercontent.com/10654512/122767450-41e00700-d2d5-11eb-8e27-17eff312edec.png)

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](https://user-images.githubusercontent.com/10654512/122767656-76ec5980-d2d5-11eb-9f5d-dd7dfed02bc7.png)

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
```go
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.