// Helper method for conn pools to convert errors func getOrPanic(pool *dbconnpool.ConnectionPool) dbconnpool.PoolConnection { conn, err := pool.Get() if err == nil { return conn } if err == dbconnpool.CONN_POOL_CLOSED_ERR { panic(connPoolClosedErr) } panic(NewTabletErrorSql(FATAL, err)) }
func (rqc *RequestContext) getConn(pool *dbconnpool.ConnectionPool) dbconnpool.PoolConnection { start := time.Now() timeout, err := rqc.deadline.Timeout() if err != nil { panic(NewTabletError(FAIL, "getConn: %v", err)) } conn, err := pool.Get(timeout) switch err { case nil: rqc.logStats.WaitingForConnection += time.Now().Sub(start) return conn case dbconnpool.CONN_POOL_CLOSED_ERR: panic(connPoolClosedErr) } panic(NewTabletErrorSql(FATAL, err)) }
// getPoolReconnect gets a connection from a pool, tests it, and reconnects if // it gets errno 2006. func getPoolReconnect(ctx context.Context, pool *dbconnpool.ConnectionPool) (dbconnpool.PoolConnection, error) { conn, err := pool.Get(ctx) if err != nil { return conn, err } // Run a test query to see if this connection is still good. if _, err := conn.ExecuteFetch("SELECT 1", 1, false); err != nil { // If we get "MySQL server has gone away (errno 2006)", try to reconnect. if sqlErr, ok := err.(*sqldb.SQLError); ok && sqlErr.Number() == 2006 { if err := conn.Reconnect(); err != nil { conn.Recycle() return conn, err } } } return conn, nil }