Skip to content
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

hope API for ConnectionID() #946

Closed
hanchuanchuan opened this issue Apr 4, 2019 · 8 comments
Closed

hope API for ConnectionID() #946

hanchuanchuan opened this issue Apr 4, 2019 · 8 comments

Comments

@hanchuanchuan
Copy link

When I create a connection, I want to get the connection ID, which may have been multiple times.
Is it possible to implement ConnectionID()?
Poor efficiency when using select connection_id() multiple times

Configuration

Go version: go 1.12

Server version: MySQL 5.7.19

Server OS: CentOS7

@methane
Copy link
Member

methane commented Apr 4, 2019

It is impossible in current database/sql design.

@hanchuanchuan
Copy link
Author

Thank you for your reply!
Can I do this like this? (#791)

// at `packets.go` `readInitPacket()` 
// connection id [4 bytes]
pos := 1 + bytes.IndexByte(data[1:], 0x00) + 1 + 4
connectionId := int(binary.LittleEndian.Uint32(data[pos-4 : pos]))

Thanks again!

@methane
Copy link
Member

methane commented Apr 4, 2019

I can't understand your question. Could you explain what you want in application?

Note that you don't use this driver directly, but uses database/sql package.
We can't add public API usable from application.

@hanchuanchuan
Copy link
Author

Whenever I execute a statement, I want to get the connection id.
The current implementation is

db.Exec("update t1 set c1=1 where id = 1")
// do something...

// get connection_id to use binlog parse
db.Query("select connection_id();")

But I have to get it many times (possibly tens of thousands of times),
so I don't want to go to the database to query the connection id every time.
Thanks!

@methane
Copy link
Member

methane commented Apr 4, 2019

Go golang/go#29835.

@kardianos
Copy link

May I ask: why do you want the Connection ID from the driver?

@hanchuanchuan
Copy link
Author

I need a Connection ID to resolve the mysql binlog log and it will execute multiple times.

@hanchuanchuan
Copy link
Author

Thank you everyone, the problem has been solved!
Upgrade the mysql driver to github.com/go-sql-driver/[email protected].
(I am sorry for the old version that I have been using before. )
All database operations use a uniform method.
When it is found that a new connection is used, it will query the connection ID once.

func (s *session) Raw(sqlStr string) (rows *sql.Rows, err error) {
    // retry 3 times
    for i := 0; i < maxBadConnRetries; i++ {
        rows, err = s.db.DB().Query(sqlStr)
        if err == nil {
            return
        } else {
            log.Error(err)
            if err == mysqlDriver.ErrInvalidConn {
                err = s.initConnection()
                if err != nil {
                    return
                }
            } else {
                return
            }
        }
    }
    return
}
func (s *session) initConnection() (err error) {
    name := "mysql"   

    // retry 3 times
    for i := 0; i < maxBadConnRetries; i++ {
        if err = s.db.Exec(fmt.Sprintf("USE `%s`", name)).Error; err == nil {
            // reset thread id
            s.threadID = 0
            return
        } else {            
            if err != mysqlDriver.ErrInvalidConn {                
                log.Error(err)
                return
            }
        }
    }

    if err != nil {
        log.Error(err)        
    }

    return
}

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

No branches or pull requests

3 participants