Variable life time -- sql.DB connection defer closing question

63 views
Skip to first unread message

Tong Sun

unread,
Jul 22, 2019, 11:35:46 AM7/22/19
to golang-nuts
All SQL DB statements need a sql.DB connection:

var conn *sql.DB = ...
defer conn
.Close()

statement
, err := conn.Prepare(SQL)


My question is, if I want to reuse the `conn` variable later on in the same function, 

conn, err = sql.Open("mssql", NewConnString)


Do I need to put another `defer conn.Close()` after it, or the first `defer conn.Close()` still applies? Or I should call `conn.Close()` explicitly beforehand? 

thanks

Burak Serdar

unread,
Jul 22, 2019, 12:14:33 PM7/22/19
to Tong Sun, golang-nuts
On Mon, Jul 22, 2019 at 9:35 AM Tong Sun <sunto...@gmail.com> wrote:
>
> All SQL DB statements need a sql.DB connection:
>
> var conn *sql.DB = ...
> defer conn.Close()
>
> statement, err := conn.Prepare(SQL)
>
>
> My question is, if I want to reuse the `conn` variable later on in the same function,
>
> conn, err = sql.Open("mssql", NewConnString)

If you didn't close conn here, then it will leak. The defer will close
the second connection, not the first.
You can add a second defer after opening the second connection. Or,
use a different variable and defer closing of that as well.

Closing the first connection manually before opening the second won't
work. The first defer will try to close the first connection. Defer
evaluates its arguments when it is declared, not when it runs.


>
>
> Do I need to put another `defer conn.Close()` after it, or the first `defer conn.Close()` still applies? Or I should call `conn.Close()` explicitly beforehand?
>
> thanks
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/11c35ccc-0713-4e09-8325-a4e608758b15%40googlegroups.com.

Robert Engels

unread,
Jul 22, 2019, 12:37:53 PM7/22/19
to Burak Serdar, Tong Sun, golang-nuts
>To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAMV2RqrkNLeBmWuFS6yE-L_VDRDZaOCrmJASP0n3g22p4rnNTg%40mail.gmail.com.

I don't think that is true. The first defer will be scheduled with the first conn arg (since it is evaluated at call time). You need another defer after the second open. You will end up with Close being called twice from defer which is what you want.

Burak Serdar

unread,
Jul 22, 2019, 12:55:57 PM7/22/19
to Robert Engels, Tong Sun, golang-nuts
I thought that's what I said as well. What part is wrong?

Robert Engels

unread,
Jul 22, 2019, 2:54:56 PM7/22/19
to Burak Serdar, Tong Sun, golang-nuts
>To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAMV2RqofVWnQJ9HrJ9NpRuP2PKcZTa%3DA6w82%2B9H3c3hR-9veuw%40mail.gmail.com.

I think it was this statement:

>> If you didn't close conn here, then it will leak. The defer will close
>> the second connection, not the first.

The first defer closes the first conn. A second defer is needed to close the second conn.

Burak Serdar

unread,
Jul 22, 2019, 2:58:25 PM7/22/19
to Robert Engels, Tong Sun, golang-nuts
You're right, my mistake. Defer will close the first, not the second.


>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1091499413.9406.1563821679403%40wamui-eagle.atl.sa.earthlink.net.
Reply all
Reply to author
Forward
0 new messages