FAQ
I came across this article on handling database connectivity with go
- http://jmoiron.net/blog/gos-database-sql/

If I have set the connection pool size to 1, then is this not possible to
do with go? (its not possible according to the article from the link above)

tx, _ := db.Begin()
r1, _ = tx.Query("SELECT * FROM foo")
r2, _ = tx.Query("SELECT * FROM bar")
tx.Commit()


  Why would transactions not allow two queries as part of the same
connection? Lets see a real use case. Transfer $10 from account A and $5
from account B into account C. One would think, it could be done this way

tx, _: db.Begin()
r1, _ = tx.Query("select * from user_account where user_id=1")
r2, _ = tx.Query("select * from user_account where user_id=2") //for
illustration, assume this cannot be collapsed into the query above i.e
"where user_id=1 or user_id=2".
// check to see if user B has enough balance to transfer $5
  // check if user A has enough balance to transfer $10
_, err := db.Exec("update user_account set account_balance=<old_balance -
10>")
if err!=nil {
     tx.Rollback()
     return err
}
_, err := db.Exec("update user_account set account_balance=<old_balance -
5> where user_id=2")
if err!=nil {
     tx.Rollback()
     return err
}
_, err := db.Exec("update user_account set account_balance=<old_balance +
15> where user_id=3")
if err!=nil {
     tx.Rollback()
     return err
}
tx.Commit()



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Ask Bjørn Hansen at Apr 6, 2014 at 11:25 pm

    On Saturday, April 5, 2014 9:55:05 PM UTC-7, sati...@gmail.com wrote:
    I came across this article on handling database connectivity with go -
    http://jmoiron.net/blog/gos-database-sql/

    If I have set the connection pool size to 1, then is this not possible to
    do with go? (its not possible according to the article from the link above)

    tx, _ := db.Begin()
    r1, _ = tx.Query("SELECT * FROM foo")
    You need to read the results and/or Close() the rows (r1) before the
    connection is made available for another query.

    http://golang.org/pkg/database/sql/#Rows.Close
    http://go-database-sql.org/retrieving.html


    Ask

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 6, '14 at 4:55a
activeApr 6, '14 at 11:25p
posts2
users2
websitegolang.org

2 users in discussion

Satishb10: 1 post Ask Bjørn Hansen: 1 post

People

Translate

site design / logo © 2022 Grokbase