title: golang database transaction: continue if single exec statement fails
body: <p>I am writing a Go app which should insert thousands of values from a file into a database. This works fine, as long as all values can be inserted into the database. If one of the queries fails, all queries afterwards fail because of <code>pq: : current transaction is aborted, commands ignored until end of transaction block</code></p>
  
  <p>I want to insert all elements and if the insert of an element fails, it should be skipped and the other elements should be inserted.</p>
  
  <p>My Code:</p>
  
  <pre><code>func (db *Database) Insert(values []Value) (transerr error) {
      tx, err := db.Begin()
      if transerr != nil {
          return nil, err
      }
      defer func() {
          if err != nil {
              tx.Rollback()
          } else {
              tx.Commit()
          }
      }
      stmt, err := tx.Prepare("INSERT INTO foo VALUES (?)")
      if err != nil {
          return err
      }
  
      defer stmt.Close()
  
      for _, value : range values {
          _, err = stmt.Exec(value)
          if err != nil {
              log.Error(err)
          }
      }
      return nil
  }
  </code></pre>
  
  <p>I tried to add a tx.Rollback() in case a stmt.Exec fails - however this results in <code>sql: statement is closed</code>. </p>