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

Use *DB.Raw(sql string, values ...interface{}) method directly occur Error 1040: Too many connections #6873

Closed
yuanybin opened this issue Mar 6, 2024 · 2 comments
Assignees
Labels
type:question general questions

Comments

@yuanybin
Copy link

yuanybin commented Mar 6, 2024

Use *DB.Raw(sql string, values ...interface{}) method directly occur Error 1040: Too many connections

gorm v1.25.1
dsn := "root:xxx@tcp(127.0.0.1:3306)/xxx?charset=utf8mb4&parseTime=True&loc=Local"
gormDb, err := gorm.Open(gormMySQL.Open(dsn))
writeConn := gormDb.WriteDB
_, err := writeConn.Raw("sql clause").Rows
when I execute writeConn.Raw("sql clause").Rows multiple times, then I will occur Error 1040: Too many connections ,
but If I change to below, then this error is disappear:

dsn := "root:xxx@tcp(127.0.0.1:3306)/xxx?charset=utf8mb4&parseTime=True&loc=Local"
gormDb, err := gorm.Open(gormMySQL.Open(dsn))
writeConn := gormDb.WriteDB
tx := db.Begin()
defer func() {
tx.Commit()
}()
_, err := tx.Raw("sql clause").Rows
I want to know why use writeConn.Raw("sql clause").Rows directly cause this error? does it open a new connection to mysql db and how can I close this single connection ?

The document you expected this should be explained

Expected answer

@yuanybin yuanybin added the type:question general questions label Mar 6, 2024
@yuanybin yuanybin changed the title Use *DB.Row(sql string, values ...interface{}) method directly occur Error 1040: Too many connections Use *DB.Raw(sql string, values ...interface{}) method directly occur Error 1040: Too many connections Mar 6, 2024
@ivila
Copy link

ivila commented Mar 8, 2024

@yuanybin When you call Rows method, you will get a *sql.Rows value, don't eat it with underscore, cause you have to call its Close method manually, you can check the sql.Rows Type, it's in official package.
change your codes to the following and it should work:

    rows, err := writeConn.Raw("sql clause").Rows()
    if (err != nil) {
        return err
    }
    defer rows.Close()

And if you just care about the err, do not care about the return value of your database, just change your codes to the following:

    // you don't have to call the Rows method.
    err := writeConn.Raw("sql clause").Err

And last, please see the design of golang official package database/sql, you have to understand the design of Connection Pool and the Close method(when calling the Close method, the connection will be returned to the Connection Pool, or drop it if it exceed MaxIdle)

@yuanybin
Copy link
Author

yuanybin commented Mar 8, 2024

thanks

@yuanybin yuanybin closed this as completed Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

3 participants