Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic in HTTP handler? #38

Open
sbowman opened this issue Feb 27, 2016 · 0 comments
Open

Panic in HTTP handler? #38

sbowman opened this issue Feb 27, 2016 · 0 comments

Comments

@sbowman
Copy link

sbowman commented Feb 27, 2016

When I include code like the following in an HTTP HandlerFunc (or its ilk), and an error occurs in the database layer (e.g. "pq: duplicate key value violates unique constraint "feeds_pkey""), the DB session doesn't close and dat panics after around 60s (killing the application):

    tx, err := svc.DB.Begin()
    if err != nil {
        http.Error(out, err.Error(), http.StatusInternalServerError)
        return
    }
    defer tx.AutoRollback()

        // "feed" was unmarshaled from a JSON document...
    if err := feed.Insert(tx); err != nil {
        http.Error(out, err.Error(), http.StatusBadRequest)
        return
    }

    if err := tx.Commit(); err != nil {
        http.Error(out, err.Error(), http.StatusInternalServerError)
        return
    }

        out.WriteHeader(http.StatusNoContent)

But if I move the transaction inside the functions, everything is fine when the error occurs (this doesn't panic):

        // "DB" is a *dat.DB
    if err := feed.Insert(DB); err != nil {
        http.Error(out, err.Error(), http.StatusBadRequest)
        return
    }

        out.WriteHeader(http.StatusNoContent)

Why isn't the "defer tx.AutoRollback()" firing and releasing the session when it's in an HTTP handler?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant