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

2 statements in 1 transaction #306

Closed
otiai10 opened this issue Oct 14, 2014 · 2 comments
Closed

2 statements in 1 transaction #306

otiai10 opened this issue Oct 14, 2014 · 2 comments

Comments

@otiai10
Copy link

otiai10 commented Oct 14, 2014

Question and need helps :(

I can execute sql below.

mydb=# BEGIN TRANSACTION;
BEGIN
mydb=# SELECT id, state FROM jobs FOR UPDATE;
                  id                  | state
--------------------------------------+-------
 4a6f9f45-537a-11e4-9298-3c15c2e679d8 | READY
(1 row)

mydb=# UPDATE jobs SET state = 'RUNNING' WHERE id = '4a6f9f45-537a-11e4-9298-3c15c2e679d8';
UPDATE 1
mydb=# COMMIT TRANSACTION ;
COMMIT
mydb=# SELECT id, state FROM jobs FOR UPDATE;
                  id                  |  state
--------------------------------------+---------
 4a6f9f45-537a-11e4-9298-3c15c2e679d8 | RUNNING
(1 row)

mydb=#

I want to realize the same thing of this, I wrote this code

package main

import (
    "fmt"
    "database/sql"
    _ "github.com/lib/pq"
)

func main() {

    db, _ := sql.Open("postgres", "host=localhost dbname=mydb sslmode=disable")
    defer db.Close()

    tx, _ := db.Begin()
    defer tx.Rollback()

    stmt1, _ := tx.Prepare("SELECT id FROM jobs WHERE state = 'READY' FOR UPDATE;")
    rows, _ := stmt1.Query()

    for rows.Next() {
        var id string
        rows.Scan(&id)

        stmt2, e := tx.Prepare("UPDATE jobs WHERE id = $1 SET state = 'RUNNING';")
        // it fails
        if e != nil {
            panic(e)
        }

        r, e := stmt2.Exec(id)
        fmt.Println(r, e)
    }

    tx.Commit()

    return
}

finally I got this error

panic: pq: unexpected describe rows response: 'D'

However I read #142 and #254 , I couldn't solve this problem.

Is there anyway to realize this by using lib/pq?

Thank you.

@johto
Copy link
Contributor

johto commented Oct 14, 2014

As for the immediate problem, you'd have to read the results of your first query somewhere before running the second one. In this case a slice of strings would work fine.

But there's no need to use FOR UPDATE in the first place; just UPDATE jobs SET state = 'running' WHERE state = 'READY' RETURNING id would work and avoid the extra round-trips caused by having to run two queries in an explicit transaction.

@otiai10
Copy link
Author

otiai10 commented Oct 14, 2014

THANK YOU VERY MUCH!!

But there's no need to use FOR UPDATE in the first place; just UPDATE jobs SET state = 'running' WHERE state = 'READY' RETURNING id would work and avoid the extra round-trips caused by having to run two queries in an explicit transaction.

The problem was that I didn't understand sql well.

I'm so ashamed... and appreciate your kindness!!

@otiai10 otiai10 closed this as completed Oct 14, 2014
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

2 participants