Skip to content

Transaction isolation levels #619

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

Merged
merged 6 commits into from
Jun 16, 2017

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 isolation-levels branch from a479c9c to 4cd9b07 Compare June 13, 2017 20:20
if err != nil {
return nil, err
}
mc.finish()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks for advice.

@zimnx zimnx force-pushed the isolation-levels branch from 9aae8e2 to 017a6c4 Compare June 14, 2017 15:53
@zimnx zimnx force-pushed the isolation-levels branch from 017a6c4 to 74b7dc8 Compare June 14, 2017 16:02
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]>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please insert your name to alphabetical order

@methane
Copy link
Member

methane commented Jun 14, 2017

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

@zimnx
Copy link
Contributor Author

zimnx commented Jun 14, 2017

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

@methane
Copy link
Member

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 commented Jun 14, 2017

Sure, thanks!

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return mc.Begin() is preferred Go style.

@shogo82148
Copy link
Contributor

thanks for your opinion.
LGTM

@bweston92
Copy link

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

@julienschmidt julienschmidt merged commit d2a8175 into go-sql-driver:master Jun 16, 2017
@julienschmidt julienschmidt added this to the v1.4 milestone Jun 16, 2017
@julienschmidt julienschmidt mentioned this pull request Jun 16, 2017
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants