Created
November 12, 2014 14:09
-
-
Save charl/3b3c2cab5fcc3fedb5d2 to your computer and use it in GitHub Desktop.
go-sql-driver/mysql stalls on locked table access
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
package main | |
import ( | |
"database/sql" | |
"fmt" | |
"log" | |
_ "github.com/go-sql-driver/mysql" | |
) | |
func main() { | |
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:3306)/%s", "root", "", "127.0.0.1", "test")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// The rest assumes a test table, foo, exists with some data in it. | |
// Create bar using foo's schema. | |
log.Print("CREATE TABLE bar LIKE foo") | |
_, err = db.Query(fmt.Sprintf("CREATE TABLE bar LIKE foo")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Lock both tables. | |
log.Print("LOCK TABLE foo WRITE, bar WRITE") | |
_, err = db.Query(fmt.Sprintf("LOCK TABLE foo WRITE, bar WRITE")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Fill bar with all the data in foo. | |
log.Print("INSERT INTO bar SELECT * FROM foo") | |
_, err = db.Query("INSERT INTO bar SELECT * FROM foo") | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Unlock the tables. | |
log.Print("UNLOCK TABLES") | |
_, err = db.Query("UNLOCK TABLES") | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The SQL used for the foo table was simply: