Comments (2)
Inlined for convenience
package main
import (
"database/sql"
)
type UserPlan struct {
User *User
Plans []Plan
}
type User struct {
Name string
}
type Plan struct {
PlanId int
}
func Insert() error {
up := &UserPlan{}
// new db
db := NewDB()
// transaction
tx, err := db.Begin()
if err != nil {
return err
}
defer func(tx *sql.Tx) {
println("init defer") // Execute here appear dead lock
if err := tx.Rollback(); err != nil {
panic(err)
}
println("end defer")
}(tx)
// insert user
if _, err := tx.Exec("insert into tb_user values($1)", up.User.Name); err != nil {
return err
}
// insert plan
for _, plan := range up.Plans {
if _, err := tx.Exec("insert into tb_plan values($1)", plan.PlanId); err != nil {
return err
}
}
// commit
if err := tx.Commit(); err != nil {
return err
}
// suc
return nil
}
func NewDB() *sql.DB {
db, err := sql.Open("postgres", "host=127.0.0.1 port=45432 dbname=viney user=viney password=admin sslmode=disable")
if err != nil {
panic(err)
}
return db
}
func main() {
if err := Insert(); err != nil {
panic(err)
}
}
from pq.
I see, I thought the crux of the weird thing going on here is you are rolling back tx
, which has likely committed....but that seems to be an error, as would seem correct. I can't reproduce this, but if you can or can provide more information, please reopen this.
My test:
func TestRollbackAfterCommit(t *testing.T) {
sentinel := &struct{}{}
defer func() {
// Absorb a specific panic to defer functionality can
// be tested.
if r := recover(); r != nil {
if r != sentinel {
panic(r)
}
}
}()
func() {
db := openTestConn(t)
defer db.Close()
tx, err := db.Begin()
defer func(tx *sql.Tx) {
if err := tx.Rollback(); err != nil {
panic(sentinel)
}
}(tx)
if err != nil {
t.Fatalf("Expected successful BEGIN, received: %v",
err)
}
// Do something during the transaction.
r := tx.QueryRow("SELECT txid_current()")
var result []byte
r.Scan(&result)
if err := tx.Commit(); err != nil {
t.Fatalf("Expected successful COMMIT, received: %v",
err)
}
}()
}
from pq.
Related Issues (20)
- Tx.Rollback returns ErrBadConn after context is cancelled
- Connection string parsing fails randomly HOT 2
- 1 Listener for 2 channel HOT 1
- SonarCloud Vulnerability: Make sure this PostgreSQL database password gets changed and removed from the code.
- Usage of COPY TO STDOUT WITH CSV
- RFC 9266: Channel Bindings for TLS 1.3 support
- Query extra_float_digits problem
- Is Postgres15.4 is compatible with the lib/pq package?
- What kind of mistake is this?
- how to deal with "cached plan must not change result type" error?
- Looking for a security contact to disclose a vulnerability HOT 1
- Exectuing pq.CopyIn fails with one of two errors depending on usage
- Why do I have errors in local language?
- pq: invalid array literal.
- pq: invalid input syntax for type integer HOT 2
- Private keys cause Trivy HIGH vulnerability alerts HOT 4
- Nil byte array doesn't get saved as JSONB field
- nil byte array `[]byte(nil)` is encoded as empty byte array `[]byte([])` instead of nil
- Extra DNS name verification is made in `verify-ca` mode, making it effectively `verify-full` HOT 1
- Got error pq: syntax error at the end of ... when doing custom dialer
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pq.