title: Sqlite3 error: database is locked in golang
body: <p>I'm using sqlite3 database in golang and I got the error: &quot;database is locked.&quot;</p>
  <p>I know there can't be multiple threads using same database file.</p>
  <p>Although I get only one connection in my program I close all query results, but it always creates 2 or 3 handles of the database file.</p>
  <p>I could check this out by using the Opendfileview program.</p>
  <p>The following code creates two database file handles.</p>
  <pre><code>func main() {
      database, tx, err := getDatabaseHandle()
      if err != nil {
          log.Fatal(err)
      }
      defer database.Close()
      dosomething(database, tx)
  }
  func dosomething(database *sql.DB, tx *sql.Tx) error {
      rows, err := database.Query(&quot;select * from sometable where name=?&quot;,&quot;some&quot;)
      if err != nil {
          return err
      }
      if rows.Next() {
          ...
      }
      rows.Close()
      //some insert queries
      tx.Commit()
  }
  func getDatabaseHandle() (*sql.DB, *sql.Tx, error) {
      database, err := sql.Open(&quot;sqlite3&quot;, dbPath)
      if err != nil {
          fmt.Println(&quot;Failed to create the handle&quot;)
          return nil, nil, err
      }
      if err2 := database.Ping(); err2 != nil {
          fmt.Println(&quot;Failed to keep connection alive&quot;)
          return nil, nil, err
      }
      tx, err := database.Begin()
      if err != nil {
          return nil, nil, err
      }
      return database, tx, nil
  }
  </code></pre>