Skip to content

auto connection problem when using go 1.2 #206

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

Closed
zx9597446 opened this issue Jan 9, 2014 · 5 comments
Closed

auto connection problem when using go 1.2 #206

zx9597446 opened this issue Jan 9, 2014 · 5 comments

Comments

@zx9597446
Copy link

hello, I found a bug when using this driver, and I'm not sure this bug is from driver or database/sql.

go version: 1.2
driver version: 1.1 (9a7aa3606b82e2081a13a008ada88dfdb96c20fd)
mysql version: mysql ver 15.1 distrib 10.0.4 - mariadb
client os: win7

bug describe in brief:

  1. Stmt.QueryRow() failed return results util next call. here are steps:
    a). prepare a Stmt with "select" query
    b). kill database connection.
    c). call Stmt.QueryRow.Scan() will fail util next call
    outputs:
[MySQL] 2014/01/09 10:27:35 packets.go:30: EOF
[MySQL] 2014/01/09 10:27:35 statement.go:24: Invalid Connection
[MySQL] 2014/01/09 10:27:35 statement.go:24: Invalid Connection
  1. Stmt.Exec failed return results, and never reconnect to database, here are steps:
    a). prepare a Stmt with "insert" query.
    b). kill database connection.
    c). call Stmt.Exec() will fail and never reconnect to database.
    outputs:
[MySQL] 2014/01/09 10:38:25 packets.go:30: EOF   <----first call Stmt.Exec()
2014/01/09 10:38:25 [::1] - �[32;1m GET /test2�[0m - 1ms
[MySQL] 2014/01/09 10:38:32 statement.go:39: Invalid Connection   <---- next call to Stmt.Exec() 
2014/01/09 10:38:32 [::1] - �[32;1m GET /test2�[0m - 1.0001ms

my test source code:

package main

import (
    "database/sql"
    "fmt"
    "math/rand"
    "time"
)
import "github.com/hoisie/web"
import _ "github.com/go-sql-driver/mysql"

import "runtime"

const sql1 = "select username from user_table where uid=?"
const sqlInsertUser = "insert into user_table set username = ?, password = ?, source=?"

var db *sql.DB
var db2 *sql.DB
var stmt *sql.Stmt
var stmt2 *sql.Stmt

func panicIf(err error) {
    if err != nil {
        panic(err)
    }
}

func init() {
    var err error
    db, err = sql.Open("mysql", "root:root@tcp(192.168.0.32:3306)/userdb")
    panicIf(err)
    //db2, err = sql.Open("mysql", "root:root@tcp(192.168.0.32:3306)/userdb")
    stmt, err = db.Prepare(sql1)
    panicIf(err)
    stmt2, err = db.Prepare(sqlInsertUser)
    panicIf(err)
    runtime.GOMAXPROCS(runtime.NumCPU())
    rand.Seed(time.Now().UnixNano())
}

func test() string {
    var username string
    stmt.QueryRow(14740).Scan(&username)

    return username
}

func test2() string {
    iu := rand.Uint32()
    ip := rand.Uint32()
    su := fmt.Sprintf("%d", iu)
    sp := fmt.Sprintf("%d", ip)
    _, err := stmt2.Exec(su, sp, "")
    if err != nil {
        return err.Error()
    }
    return "Ok"
}

func main() {
    web.Get("/", test)
    web.Get("/test2", test2)
    web.Run("0.0.0.0:80")
}
@julienschmidt
Copy link
Member

Looks a lot like http://golang.org/issue/5718
This should be fixed in Go tip: https://code.google.com/p/go/source/detail?r=3662d56e2402

@zx9597446
Copy link
Author

to solve the problem, should I build go from source myself?
any other ways?

@arnehormann
Copy link
Member

Yes you should and no, there's no way not involving waiting unless you find a packaged version of tip for your OS.

@julienschmidt
Copy link
Member

You could just update the database/sql package (just replace the source files in $GOROOT/src/pkg/database/sql). Don't forget to delete $GOROOT/pkg/database/sql, otherwise the cached package version is used.

@zx9597446
Copy link
Author

ok, update to tip solved the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants