Golang, mysql: Error 1040: Too many connections

前端 未结 5 1754
终归单人心
终归单人心 2021-02-01 17:57

I\'m using the github.com/go-sql-driver/mysql driver for go.

I open a database:

db, err := sql.Open(\"mysql\", str)

Then I have two fun

5条回答
  •  不要未来只要你来
    2021-02-01 18:33

    My program is connecting always to database. (Realtime Face Recognition for Attendance)

    Therefore opening and closing database connection is worthless.

    Therefore it's keep opens the database connection only initializing the program.

    func GetAllFaces() interface{} {
        OpenDatabaseConnection() ... 
    }
    

    But access database later, increased the no of connection and crashed the program. But closing the rows object kept no of active connection at minimum. (for me 1)

     func SaveAttendance(faceId int, date time.Time) error {
        sqlQuery := fmt.Sprintf("SELECT ... "))
    
        rows, err := DB.Query(sqlQuery) ...
        err = rows.Close()
        return err
    }
    

提交回复
热议问题