mysql: ExecContext should return error if it is canceled during execution

Issue description

mysqlConn.ExecContext uses mysqlConn.watchCancel to decide if the ctx passed as an argument is canceled, so if the ctx is canceled before the decision, it returns an error. However, mysqlConn.ExecContext does not return an error even if the ctx is cancelled during or after mysqlConn.Exec execution, or during mysqlConn.finish execution in defer. Is this the intended behavior?

Configuration

Driver version (or git SHA):

Go version: 1.17.2

Server version: E.g. MySQL 5.7

Server OS: amazonlinux:latest(Docker Image)

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 21 (13 by maintainers)

Most upvoted comments

I think so, but the reason I use ExecContext is because I want to link requests and queries when tracing an application.

Oh. It would be better to add an option to omit context cancel support for such use cases. It can reduce the number of goroutines too, because we don’t need watcher anymore.

OK, now I understand it.

db.BeginTx() takes Background(), not ctx. And tx.Commit() don’t take ctx too. So you want to avoid error caused by ctx cancel returned from tx.Commit().

That is definitely different from ctx is cancelled during database/sql or elsewhere, because mc.finish() marks it is bad connection.

In regular (e.g. autocommit) case, I don’t want to throw away results when Exec() succeeded but connection is marked but by sad timing. Query is executed surely.

On the other hand, in the transactional case, you are right. Although we need to care about Commit error, I want to avoid it as possible.