Ejemplo n.º 1
0
func (c *Conn) ReConnect() error {
	if c.conn != nil {
		c.conn.Close()
	}

	n := "tcp"
	if strings.Contains(c.addr, "/") {
		n = "unix"
	}

	netConn, err := net.Dial(n, c.addr)
	if err != nil {
		return err
	}

	tcpConn := netConn.(*net.TCPConn)

	//SetNoDelay controls whether the operating system should delay packet transmission
	// in hopes of sending fewer packets (Nagle's algorithm).
	// The default is true (no delay),
	// meaning that data is sent as soon as possible after a Write.
	//I set this option false.
	tcpConn.SetNoDelay(false)
	tcpConn.SetKeepAlive(true)
	c.conn = tcpConn
	c.pkg = mysql.NewPacketIO(tcpConn)

	if err := c.readInitialHandshake(); err != nil {
		c.conn.Close()
		return err
	}

	if err := c.writeAuthHandshake(); err != nil {
		c.conn.Close()

		return err
	}

	if _, err := c.readOK(); err != nil {
		c.conn.Close()

		return err
	}

	//we must always use autocommit
	if !c.IsAutoCommit() {
		if _, err := c.exec("set autocommit = 1"); err != nil {
			c.conn.Close()

			return err
		}
	}

	return nil
}
Ejemplo n.º 2
0
func newSession(rw net.Conn, id uint32, server *Server) (*session, error) {
	sei := &session{
		rw:        rw,
		id:        id,
		salt:      mysql.ScrambleBuf(20),
		pkg:       mysql.NewPacketIO(rw),
		status:    mysql.SERVER_STATUS_AUTOCOMMIT,
		charset:   mysql.DEFAULT_CHARSET,
		collation: mysql.DEFAULT_COLLATION_ID,
		server:    server,
	}
	return sei, nil
}