title: Sqlite database locked
body: <p>I'm using asp.net c# and upload a SqLite database to a server and then I do some inserting and updating. The problem is that sometimes (I think it's when somethings go wrong with the updating or so) the database gets locked. So the next time I try to upload a file again it's locked and I get an error saying "The process cannot access the file because it is being used by another process". Maybe the database file isn't disposed if something goes wrong during the transaction? The only thing to solve this problem is restarting the server. </p>
  
  <p>How can I solve it in my code so I can be sure it's always unlocked even if something goes wrong?</p>
  
  <p>This is my code:</p>
  
  <pre><code>try
  {
    string filepath = Server.MapPath("~/files/db.sql");
  
    //Gets the file and save it on the server
    ((HttpPostedFile)HttpContext.Current.Request.Files["sqlitedb"]).SaveAs(filepath);
  
    //Open the database
    SQLiteConnection conn = new SQLiteConnection("Data Source=" + filepath + ";Version=3;");
  
    conn.Open();
    SQLiteCommand cmd = new SQLiteCommand(conn);
    using (SQLiteTransaction transaction = conn.BeginTransaction())
    {
       using (cmd)
       {
          //Here I do some stuff to the database, update, insert etc
       }
       transaction.Commit();
    }
    conn.Close();
    cmd.Dispose();
  }
  catch (Exception exp)
  {
  //Error
  }
  </code></pre>