Recently I have developed a few web applications in Go, using a PostgreSQL 
database, but I have yet understand the best way to use the database/sql 
package.

The simplest method seems to start a transaction in the HTTP handler and 
pass a *sql.Tx value to all the functions that need to access the database.
However I'm afraid that this method is "too" simple.

One possible problem is an HTTP handler acting as a middleware, and both 
the "middleware" an the "normal" HTTP handler needs a transaction.
Using the "simplest method", the middleware and the handler will end up 
using two distinct transaction, and this may not be what one expects.


The other solution is to do what frameworks like Django do, storing 
something like

  type Interface interface {
      func Exec(query string, ...) ...
      func Query(query string, ...) ...
      func InTransaction) bool
  }

as a per request data, including transaction state, (ab)using 
Context.WithValue.  But I suspect that this is not a good idea.


Thanks  Manlio

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to