-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
af2da39
Added Gogland IDE internal directory to .gitignore
4cd9b07
Support transaction isolation level in BeginTx
0d8f878
Review fixes
74b7dc8
Simplyfied TestContextBeginIsolationLevel test
06cbc7a
Applied more review comments
62836ef
Applied review remarks
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
Icon? | ||
ehthumbs.db | ||
Thumbs.db | ||
.idea |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ Lion Yang <lion at aosc.xyz> | |
Luca Looz <luca.looz92 at gmail.com> | ||
Lucas Liu <extrafliu at gmail.com> | ||
Luke Scott <luke at webconnex.com> | ||
Maciej Zimnoch <[email protected]> | ||
Michael Woolnough <michael.woolnough at gmail.com> | ||
Nicola Peduzzi <thenikso at gmail.com> | ||
Olivier Mengué <dolmen at cpan.org> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -468,3 +468,55 @@ func TestContextCancelBegin(t *testing.T) { | |
} | ||
}) | ||
} | ||
|
||
func TestContextBeginIsolationLevel(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, thanks for advice. |
||
runTests(t, dsn, func(dbt *DBTest) { | ||
dbt.mustExec("CREATE TABLE test (v INTEGER)") | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
tx1, err := dbt.db.BeginTx(ctx, &sql.TxOptions{ | ||
Isolation: sql.LevelRepeatableRead, | ||
}) | ||
if err != nil { | ||
dbt.Fatal(err) | ||
} | ||
|
||
tx2, err := dbt.db.BeginTx(ctx, &sql.TxOptions{ | ||
Isolation: sql.LevelReadCommitted, | ||
}) | ||
if err != nil { | ||
dbt.Fatal(err) | ||
} | ||
|
||
_, err = tx1.ExecContext(ctx, "INSERT INTO test VALUES (1)") | ||
if err != nil { | ||
dbt.Fatal(err) | ||
} | ||
|
||
var v int | ||
row := tx2.QueryRowContext(ctx, "SELECT COUNT(*) FROM test") | ||
if err := row.Scan(&v); err != nil { | ||
dbt.Fatal(err) | ||
} | ||
// Because writer transaction wasn't commited yet, it should be available | ||
if v != 0 { | ||
dbt.Errorf("expected val to be 0, got %d", v) | ||
} | ||
|
||
err = tx1.Commit() | ||
if err != nil { | ||
dbt.Fatal(err) | ||
} | ||
|
||
row = tx2.QueryRowContext(ctx, "SELECT COUNT(*) FROM test") | ||
if err := row.Scan(&v); err != nil { | ||
dbt.Fatal(err) | ||
} | ||
// Data written by writer transaction is already commited, it should be selectable | ||
if v != 1 { | ||
dbt.Errorf("expected val to be 1, got %d", v) | ||
} | ||
tx2.Commit() | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package | ||
// | ||
// Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
// +build go1.8 | ||
|
||
package mysql | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"testing" | ||
) | ||
|
||
func TestIsolationLevelMapping(t *testing.T) { | ||
|
||
data := []struct { | ||
level driver.IsolationLevel | ||
expected string | ||
}{ | ||
{ | ||
level: driver.IsolationLevel(sql.LevelReadCommitted), | ||
expected: "READ COMMITTED", | ||
}, | ||
{ | ||
level: driver.IsolationLevel(sql.LevelRepeatableRead), | ||
expected: "REPEATABLE READ", | ||
}, | ||
{ | ||
level: driver.IsolationLevel(sql.LevelReadUncommitted), | ||
expected: "READ UNCOMMITTED", | ||
}, | ||
{ | ||
level: driver.IsolationLevel(sql.LevelSerializable), | ||
expected: "SERIALIZABLE", | ||
}, | ||
} | ||
|
||
for i, td := range data { | ||
if actual, err := mapIsolationLevel(td.level); actual != td.expected || err != nil { | ||
t.Fatal(i, td.expected, actual, err) | ||
} | ||
} | ||
|
||
// check unsupported mapping | ||
if actual, err := mapIsolationLevel(driver.IsolationLevel(sql.LevelLinearizable)); actual != "" || err == nil { | ||
t.Fatal("Expected error on unsupported isolation level") | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.