Coder Social home page Coder Social logo

Operation transaction, found a bug. about pq HOT 2 CLOSED

lib avatar lib commented on June 14, 2024
Operation transaction, found a bug.

from pq.

Comments (2)

fdr avatar fdr commented on June 14, 2024

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.

fdr avatar fdr commented on June 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.