title: Wrapping a db object in Go and running two methods in the same transaction
body: <p>In the effort of learning Go a bit better, I am trying to refactor a series of functions which accept a DB connection as the first argument into struct methods and something a bit more "idiomatically" Go.</p>
  
  <p>Right now my "data store" methods are something like this:</p>
  
  <pre class="lang-go prettyprint-override"><code>func CreateA(db orm.DB, a *A) error {
      db.Exec("INSERT...")
  }
  
  func CreateB(db orm.DB, b *B) error {
      db.Exec("INSERT...")
  }
  </code></pre>
  
  <p>These the functions work perfectly fine. <code>orm.DB</code> is the <a href="https://godoc.org/github.com/go-pg/pg/orm#DB" rel="noreferrer">DB interface of go-pg</a>.</p>
  
  <p>Since the two functions accept a db connection I can either pass an actual connection or a <a href="https://godoc.org/github.com/go-pg/pg#Tx" rel="noreferrer">transaction</a> (which implements the same interface). I can be sure that both functions issuing SQL INSERTs run in the same transaction, avoiding having inconsistent state in the DB in case either one of them fails.</p>
  
  <p>The trouble started when I decided to read more about how to structure the code a little better and to make it "mockable" in case I need to. </p>
  
  <p>So I googled a bit, read the article <a href="http://www.alexedwards.net/blog/organising-database-access" rel="noreferrer">Practical Persistence in Go: Organising Database Access</a> and tried to refactor the code to use proper interfaces.</p>
  
  <p>The result is something like this:</p>
  
  <pre class="lang-go prettyprint-override"><code>type Store {
      CreateA(a *A) error
      CreateB(a *A) error
  }
  
  type DB struct {
      orm.DB
  }
  
  func NewDBConnection(p *ConnParams) (*DB, error) {
      .... create db connection ...
      return &amp;DB{db}, nil
  }
  
  func (db *DB) CreateA(a *A) error {
  ...
  }
  
  func (db *DB) CreateB(b *B) error {
  ...
  }
  </code></pre>
  
  <p>which allows me to write code like:</p>
  
  <pre class="lang-go prettyprint-override"><code>db := NewDBConnection()
  DB.CreateA(a)
  DB.CreateB(b)
  </code></pre>
  
  <p>instead of:</p>
  
  <pre class="lang-go prettyprint-override"><code>db := NewDBConnection()
  CreateA(db, a)
  CreateB(db, b)
  </code></pre>
  
  <p>The actual issue is that I lost the ability to run the two functions in the same transaction. Before I could do:</p>
  
  <pre class="lang-go prettyprint-override"><code>pgDB := DB.DB.(*pg.DB) // convert the interface to an actual connection
  pgDB.RunInTransaction(func(tx *pg.Tx) error {
      CreateA(tx, a)
      CreateB(tx, b)
  })
  </code></pre>
  
  <p>or something like:</p>
  
  <pre class="lang-go prettyprint-override"><code>tx := db.DB.Begin()
  
  err = CreateA(tx, a)
  err = CreateB(tx, b)
  
  if err != nil {
    tx.Rollback()
  } else {
    tx.Commit()
  }
  </code></pre>
  
  <p>which is more or less the same thing.</p>
  
  <p>Since the functions were accepting the common interface between a connection and a transaction I could abstract from my model layer the transaction logic sending down either a full connection or a transaction. This allowed me to decide in the "HTTP handler" when to create a trasaction and when I didn't need to.</p>
  
  <p>Keep in mind that the connection is a global object representing a pool of connections handled automatically by go, so the hack I tried:</p>
  
  <pre class="lang-go prettyprint-override"><code>pgDB := DB.DB.(*pg.DB) // convert the interface to an actual connection
  err = pgDB.RunInTransaction(func(tx *pg.Tx) error {
      DB.DB = tx // replace the connection with a transaction
      DB.CreateA(a)
      DB.CreateB(a)
  })
  </code></pre>
  
  <p>it's clearly a bad idea, because although it works, it works only once because we replace the global connection with a transaction. The following request breaks the server.</p>
  
  <p>Any ideas? I can't find information about this around, probably because I don't know the right keywords being a noob.</p>