So... this was apperantly an issue a few years ago, atleast there are several bugs with what I think is the same issue. In a method I have 2 transactions, one after the other (so they are not concurrent, they are sequencial) and the second transaction always fails.
This is roughly the code:
import (
"database/sql"
_ "github.com/lib/pq"
)
var db *sql.DB
func InitStorage() {
var err error
db, err = sql.Open(os.Getenv("DB_CONNECTION_DRIVER"), os.Getenv("DB_CONNECTION_STRING"))
if err != nil {
glog.Error(err)
}
if db == nil {
glog.Fatal(db)
}
tx, err := db.Begin()
if err != nil {
glog.Error(err)
} else {
glog.Info("ERROR ON BEGIN IS NiLL!!!")
}
_, err = tx.Query(`INSERT INTO urls(url_hash, url) VALUES($1, $2)`, `asdadsaaa`, `1313`)
if err != nil {
glog.Error(err)
tx.Rollback()
}
tx.Commit()
glog.Info("SECOND TRANSACTION!!!")
tx1, err := db.Begin()
if err != nil {
glog.Error("SECOND TRANSACTION HAS ERR: ", err)
} else {
glog.Info("ERROR ON BEGIN IS NiLL!!!")
}
_, err = tx1.Query(`INSERT INTO urls(url_hash, url) VALUES($1, $2)`, `asdadsaaa`, `1313`)
if err != nil {
glog.Error(err)
tx1.Rollback()
}
tx1.Commit()
}
And this is always the output (I have cut the end, since it doesn't bring useful information):
I0324 00:24:26.192256 11580 storage_rdb.go:33] ERROR ON BEGIN IS NiLL!!!
I0324 00:24:26.195134 11580 storage_rdb.go:42] SECOND TRANSACTION!!!
E0324 00:24:26.195197 11580 storage_rdb.go:45] SECOND TRANSACTION HAS ERRpq: unexpected transaction status idle in transaction
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x5460cb]
goroutine 1 [running]:
panic(0x7c41c0, 0xc820010150)
/usr/local/go/src/runtime/panic.go:464 +0x3e6
database/sql.(*Tx).Query(0x0, 0x8b2e00, 0x2e, 0xc82011fd50, 0x2, 0x2, 0xc82001a540, 0x0, 0x0)
/usr/local/go/src/database/sql/sql.go:1404 +0x3b
main.InitStorage()
I assume I am doing something wrong(since the bug was found in 2013), but I can't figure out what it is. Please help.