The Wayback Machine - https://web.archive.org/web/20210202123431/https://github.com/go-sql-driver/mysql/pull/619
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 isolation levels #619

Merged
merged 6 commits into from Jun 16, 2017
Merged

Conversation

@zimnx
Copy link
Contributor

@zimnx zimnx commented Jun 13, 2017

Description

Support setting transaction isolation level

Currently supported isolation levels are:

Checklist

  • Code compiles correctly
  • Created tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary
  • Added myself / the copyright holder to the AUTHORS file
@zimnx zimnx force-pushed the zimnx:isolation-levels branch from a479c9c to 4cd9b07 Jun 13, 2017
if err != nil {
return nil, err
}
mc.finish()

This comment has been minimized.

@methane

methane Jun 14, 2017
Member

You shouldn't use mc.finish() here. It should be paried with mc.watchCancel().
Move whole this if statement after watchCancel(), and remove mc.finish().

This comment has been minimized.

@shogo82148

shogo82148 Jun 14, 2017
Contributor

Move whole this if statement after watchCancel(), and remove mc.finish().

In addition, please don't forget to call mc.finish() before return nil, err.
mc.finish() must called before all return.

@zimnx
Copy link
Contributor Author

@zimnx zimnx commented Jun 14, 2017

@methane @shogo82148 I've applied your comments. Do you guys think defering mc.finish() after watchCancel() is OK?

@shogo82148
Copy link
Contributor

@shogo82148 shogo82148 commented Jun 14, 2017

@zimnx not ok.
because mc.finish() should be called before tx.Rollback().

... but, calling tx.Rollback() may be very rare case.
so, if you delete the following code, and then you can add defer mc.finish()

select {
default:
case <-ctx.Done():
	tx.Rollback()
	return nil, ctx.Err()
}

this code comes from ctxutil.go.

@methane How do you think which is better calling tx.Rollback() in BeginTx, or defering mc.finish()?

@@ -468,3 +469,100 @@ func TestContextCancelBegin(t *testing.T) {
}
})
}

func TestContextBeginIsolationLevel(t *testing.T) {

This comment has been minimized.

@shogo82148

shogo82148 Jun 14, 2017
Contributor

nice test👍

but, this test can be written more concisely, can't it?
for example

tx1, err := dbt.db.BeginTx(ctx, &sql.TxOptions{
    Isolation: sql.LevelRepeatableRead,
})
tx2, err := dbt.db.BeginTx(ctx, &sql.TxOptions{
    Isolation: sql.LevelReadCommitted,
})

row := tx2.QueryRowContext(ctx, "SELECT COUNT(*) FROM test")
// check row.

_, err = tx1.ExecContext(ctx, "INSERT INTO test VALUES (1)")

row = tx2.QueryRowContext(ctx, "SELECT COUNT(*) FROM test")
// check row.

This comment has been minimized.

@zimnx

zimnx Jun 14, 2017
Author Contributor

Done, thanks for advice.

@zimnx zimnx force-pushed the zimnx:isolation-levels branch from 9aae8e2 to 017a6c4 Jun 14, 2017
Maciej Zimnoch added 2 commits Jun 14, 2017
@zimnx zimnx force-pushed the zimnx:isolation-levels branch from 017a6c4 to 74b7dc8 Jun 14, 2017
AUTHORS Outdated
@@ -61,6 +61,7 @@ Xiangyu Hu <xiangyu.hu at outlook.com>
Xiaobing Jiang <s7v7nislands at gmail.com>
Xiuming Chen <cc at cxm.cc>
Zhenye Xie <xiezhenye at gmail.com>
Maciej Zimnoch <[email protected]>

This comment has been minimized.

@methane

methane Jun 14, 2017
Member

Please insert your name to alphabetical order

@methane
Copy link
Member

@methane methane commented Jun 14, 2017

@shogo82148 +1 to just remove it.
The ctx can be cancelled right after the select statement anyway.

Maciej Zimnoch
@zimnx
Copy link
Contributor Author

@zimnx zimnx commented Jun 14, 2017

@methane Done. Do you have any more comments? I would like to squash commits.

@methane
Copy link
Member

@methane methane commented Jun 14, 2017

No need to manual squash. We use "Squash and merge".

I'll review whole pull request again in tomorrow. Please wait.

@zimnx
Copy link
Contributor Author

@zimnx zimnx commented Jun 14, 2017

Sure, thanks!

case <-ctx.Done():
tx.Rollback()
return nil, ctx.Err()
}
return tx, err

This comment has been minimized.

@methane

methane Jun 14, 2017
Member

return mc.Begin() is preferred Go style.

@shogo82148
Copy link
Contributor

@shogo82148 shogo82148 commented Jun 15, 2017

thanks for your opinion.
LGTM

@bweston92
Copy link

@bweston92 bweston92 commented Jun 15, 2017

Any news on this? Literally just got an error about this not working.

Maciej Zimnoch
@julienschmidt julienschmidt merged commit d2a8175 into go-sql-driver:master Jun 16, 2017
2 checks passed
2 checks passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+0.2%) to 75.669%
Details
@julienschmidt julienschmidt added this to the v1.4 milestone Jun 16, 2017
@julienschmidt julienschmidt mentioned this pull request Jun 16, 2017
5 of 5 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants