Skip to content

Will connections be leaked when use transactions? #691

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

Closed
kof02guy opened this issue Oct 26, 2017 · 2 comments
Closed

Will connections be leaked when use transactions? #691

kof02guy opened this issue Oct 26, 2017 · 2 comments

Comments

@kof02guy
Copy link

Issue description

I used DB.Begin() to start a transaction and DB.Commit() or DB.Rollback() to end transaction, but I found some connections in the status of "SLEEP" for quite a long time (> 2 hours). And this is not expected. So do I miss something to make sure the connections are all to be freed?

Example code

mgr.statsConn, err = sql.Open("mysql", statsConnStr)
......
tx, err := mgr.statsConn.Begin()
if err != nil {
LogError(FAIL_CRITICAL, DB, "Begin transaction failed:%v", err)
return err
}
defer tx.Rollback()
...
multiple Exec clauses just like:
rst, err = tx.Exec(sqlStr)
if err != nil {
LogInfo("Update xxx error. Sql:%s, Error:%v", sqlStr, err)
return err
}
...
if err = tx.Commit(); nil != err {
LogError(FAIL_CRITICAL, DB, "Commit error:%v", err)
return err
}

sql.Open() will be called only once of the entire lifetime of the process. and mgr.statsConn will be used everytime when opening a transaction
I'm wondering the right position for the tx.Rollback(). If error happens, is it necessary to call tx.Rollback()? Will tx be non-nill then?

Error log

If you have an error log, please paste it here.

Configuration

Driver version (or git SHA):

Go version: run go version in your console
1.7.5

Server version: E.g. MySQL 5.6, MariaDB 10.0.20
MYSQL 5.5.37-enterprise-commercial-advanced-log

Server OS: E.g. Debian 8.1 (Jessie), Windows 10

@busyfree
Copy link

busyfree commented Nov 20, 2017

@kof02guy I have the same problem with mysql connection socket leak, I don't know what's the reason to occured this problem. see #707
wx20171120-125723

from wait_closed to can't identify protocol
image

@methane
Copy link
Member

methane commented Jan 8, 2018

tx.Commit() and Rollback() returns connection to the pool.

I recommend DB.SetConnMaxLifetime(time.Second * 10) for everyone except experts
who can solve problem by themselves.

@methane methane closed this as completed Jan 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants