st.Close() is not working when I inherit sql.st

I inherited sql.st and then tried to run st.close but it was producing go routine error. I did not override the close function and I did not make any changes why it is producing the error.

Code:-

package main

import (
    _"database/sql"
    "fmt"
    a "Vnext/go_ibm_db"
)

func main() {
    con := "HOSTNAME=localhost;PORT=50000;DATABASE=go;UID=uname;PWD=pass"
    mydb, err := a.Open("go_ibm_db", con)
    if err != nil {
        fmt.Println(err)
    }
	defer mydb.Close()
    myst,err:=mydb.Prepare("Insert into putdata(a,b) values(?,?)")
	if err != nil{
	fmt.Println(err)
	}
	myst.Query("akhil","ravuri")
	myst.Close()
}

Package Code:-

type DBd struct {
	sql.DB
}

func Open(driver string, ConStr string) (*DBd, error) {
	db, err := sql.Open(driver, ConStr)
	if err != nil {
		return nil, err
	}
	dbd := &DBd{
		DB: *db,
	}
	return dbd, nil
}

type Stmtd struct {
	sql.Stmt
	Hand api.SQLHSTMT
}

func (db *DBd) Prepare(query string) (*Stmtd, error) {
	st, err := db.DB.Prepare(query)
	if err != nil {
		return nil, err
	}
	std := &Stmtd{
		Stmt: *st,
		Hand: Handle,
	}
	return std, nil
}

Error:-

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [semacquire]:
sync.runtime_SemacquireMutex(0xc0000881a4, 0x0)
        C:/Go/src/runtime/sema.go:71 +0x44
sync.(*Mutex).Lock(0xc0000881a0)
        C:/Go/src/sync/mutex.go:134 +0x106
database/sql.(*DB).Close(0xc000088180, 0x44, 0xc000088180)
        C:/Go/src/database/sql/sql.go:791 +0xa4
panic(0x4c95c0, 0xc000030060)
        C:/Go/src/runtime/panic.go:513 +0x1c7
database/sql.(*DB).removeDepLocked(0xc000088180, 0x50b6a0, 0xc0000b4140, 0x4e4760, 0xc0000b4140, 0x0)
        C:/Go/src/database/sql/sql.go:629 +0x3e3
database/sql.(*DB).removeDep(0xc000088180, 0x50b6a0, 0xc0000b4140, 0x4e4760, 0xc0000b4140, 0x0, 0x2)
        C:/Go/src/database/sql/sql.go:619 +0xba
database/sql.(*Stmt).Close(0xc0000b4140, 0x0, 0x0)
        C:/Go/src/database/sql/sql.go:2674 +0x1eb
main.main()
        C:/Users/rakhil/Desktop/st_close.go:21 +0x1c8

goroutine 19 [select]:
database/sql.(*DB).connectionOpener(0xc0000880c0, 0x50bac0, 0xc000046100)
        C:/Go/src/database/sql/sql.go:1042 +0x132
created by database/sql.OpenDB
        C:/Go/src/database/sql/sql.go:698 +0x1b5

goroutine 20 [select]:
database/sql.(*DB).connectionResetter(0xc0000880c0, 0x50bac0, 0xc000046100)
        C:/Go/src/database/sql/sql.go:1056 +0x148
created by database/sql.OpenDB
        C:/Go/src/database/sql/sql.go:699 +0x1eb
exit status 2

can anyone help me on this?
Thanks.

When I removed defer mydb.Close and place mydb.Close() after myst.Close() the error is:-

panic: unpaired removeDep: no deps for *sql.Stmt

goroutine 1 [running]:
database/sql.(*DB).removeDepLocked(0xc000088180, 0x50c740, 0xc0000421e0, 0x4e5760, 0xc0000421e0, 0x0)
        C:/Go/src/database/sql/sql.go:629 +0x3e3
database/sql.(*DB).removeDep(0xc000088180, 0x50c740, 0xc0000421e0, 0x4e5760, 0xc0000421e0, 0x0, 0xc00006fee8)
        C:/Go/src/database/sql/sql.go:619 +0xba
database/sql.(*Stmt).Close(0xc0000421e0, 0x0, 0x0)
        C:/Go/src/database/sql/sql.go:2674 +0x1eb
main.main()
        C:/Users/rakhil/Desktop/SQLPutData.go:40 +0x25e
exit status 2

Thanks @johandalabacka for your input.

I have figured it out. Just changed the Stmtd stuct decleration from sql.Stmt to *sql.Stmt
It worked.

Thanks.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.