I have daemon which continuously inserting data in mysql and returning me to LastInsertId()
. When i start daemon its working perfect for first approx 150 000 entries. After that execution get stopped with following error
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x45af73]
Here is my code snippet:
insert, _ := db.Prepare("insert into info set id=?,tg=?, adddate=now() ON DUPLICATE KEY UPDATE tgs=1")
for {
res, _ := insert.Exec(id, tg)
insertid, _ := res.LastInsertId() //Getting error on this line
}
I am running 10 GOROUTINES, Its gives same error with single goroutine . Also i tried with table lock and unlock before and after query. It gives same error.
res.RowsAffected()
This also not giving any output.
In for loop i just did fmt.Print(insert)
. It gives me memory address in array. Which keep increasing . After some point it got breaks. Here it is:
[{0xc4200d6070 0xc4200e0100} {0xc4200d6150 0xc42005e040} {0xc4200d61c0 0xc42005e0c0} {0xc4200d6230 0xc4203e04c0} {0xc4200d62a0 0xc4201585c0} {0xc4200d6310 0xc420469280} {0xc4200d6380 0xc420158200} {0xc4200d63f0 0xc42005fe00} {0xc4200d6460 0xc420468840} {0xc4200d64d0 0xc4200c0cc0} {0xc4200d6540 0xc4200e0a40} {0xc4200d65b0 0xc42005f480} {0xc4200d6620 0xc4200c1a80} {0xc4200d6690 0xc420254bc0} {0xc4200d6700 0xc4200e0200}] 0}insert into trans_tag_master set clientid=69,tag='tag1', adddate=now() ON DUPLICATE KEY UPDATE tagstatus=1
The memory is keep increasing and after some point it exceed limit of prepare statement. How i can avoid this. Thanks
It may be silly question but i would like to answer of my own question. Got answer from https://github.com/go-sql-driver/mysql/issues/743 . Go-mysql library was making connection continuously. Because of which i got connection refused error. @Carpetsmoker @jimB you guys was correct i was ignoring the error. In error i got message connection refused
. I also posted whole code on above link. I set below param in my code:
DB.SetMaxIdleConns(10)
DB.SetMaxOpenConns(10)
DB.SetConnMaxLifetime(time.Second * 10)
It start works. Now its not closing or keeps connected for given time. Thanks guys for your support.