You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This package will properly implement context cancelation for MySQL. Without this package, context cancelation does not actually cancel a MySQL query.
See Article for details of the behind-the-scenes magic.
The API is designed to resemble the standard library. It is fully compatible with the dbq package which allows for zero boilerplate database operations in Go.
import (
sql "github.com/rocketlaunchr/mysql-go"
)
pool, _:=sql.Open("user:password@tcp(localhost:3306)/db")
Read Query
// Obtain an exclusive connectionconn, err:=pool.Conn(ctx)
deferconn.Close() // Return the connection back to the pool// Perform your read operation.rows, err:=conn.QueryContext(ctx, stmt)
iferr!=nil {
returnerr
}
Write Query
// Obtain an exclusive connectionconn, err:=pool.Conn(ctx)
deferconn.Close() // Return the connection back to the pool// Perform the write operationtx, err:=conn.BeginTx(ctx, nil)
_, err=tx.ExecContext(ctx, stmt)
iferr!=nil {
returntx.Rollback()
}
tx.Commit()
Cancel Query
Cancel the context. This will send a KILL signal to MySQL automatically.
It is highly recommended you set a KillerPool when you instantiate the DB object.
The KillerPool is used to call the KILL signal.
Reverse Proxy Support
Checkout the proxy-protection branch if your database is behind a reverse proxy in order to better guarantee that you are killing the correct query.