Skip to content

Instantly share code, notes, and snippets.

@charl
Created November 12, 2014 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charl/3b3c2cab5fcc3fedb5d2 to your computer and use it in GitHub Desktop.
Save charl/3b3c2cab5fcc3fedb5d2 to your computer and use it in GitHub Desktop.
go-sql-driver/mysql stalls on locked table access
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)
}
}
@charl
Copy link
Author

charl commented Nov 12, 2014

The SQL used for the foo table was simply:

mysql> CREATE TABLE foo (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, data VARCHAR(100));
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO foo (data) VALUES ('Hello world');
Query OK, 1 row affected (0.01 sec)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment