简体   繁体   中英

Do we need to close SQLite connection when exiting WP app?

In the App.xaml.cs of my universal app I have opened an SQLConnection. I'm not closing the connection. So far it hasn't caused me trouble. But is it the right way, can we leave it like that?

If we are to close the connection ( Dispose() because I don't see a close function) where do we close? In the App_Closing ?

EDIT

This is how I'm opening the connection in App.xaml.cs

public static SQLiteConnection conn;
public void LoadDatabase()
    {
        conn = new SQLiteConnection( "JusWriteDB.db");

        string sql = @"CREATE TABLE IF NOT EXISTS Folder (FolderCompletedStatus INTEGER, FolderPriority INTEGER, PenColor INTEGER,                                                         FolderText TEXT, FolderUUID TEXT PRIMARY KEY NOT NULL );";
        using (var statement = conn.Prepare(sql))
        {
            try
            {
                statement.Step();
            }
            catch(Exception)
            {

            }
        }
    }

And where ever(in some other file) I need a to access this connection, to update table or insert. I access it through the public variable defined in App.xaml.cs

 var db = App.conn;
 string sql = "SELECT * from Folder Where FolderSyncStatus = 'Del'; ";
 try
    {
      using (var statement = db.Prepare(sql))
      {
         while (statement.Step() == SQLiteResult.ROW)
         { //  code 
         }
      }
    }
  catch{}

The general rule is to always invoke Dispose() method on IDisposalbe objects. So yes, you should close it. The next question is where. There are two ways of doing it. First - use using directive for a temporary local connection, eg:

using(SqlConnection connection = new SqlConnection("ConnectionString"))
{
    // some code here
}

But is seems that you store a reference to the connection in a field. In this case, the class that has this field should implement IDisposable and invoke connection.Dispose() in its own Dispose method:

class Repository : IDisposable
{
    SqlConnection connection = new SqlConnection("ConnectionString");

    void Dispose()
    {
        connection.Dispose();
    }
}

But anyway you will need using here, the only difference - in using you will create Repository instead of SqlConnection

Do we need to close SQLite connection when exiting WP app?

The general rule is: If an object implements IDisposable , you should Dispose it or use a using .

using(SQLiteConnection conn = new SQLiteConnection("<ConnectionString>"))
{
...
}

There are only a few exceptions where it depends (like DataSets and DataTables ).

If we are to close the connection (Dispose() cuz I dont see a close function) where do we close?

You don't have to ask this question if you use a using , because it will be Disposed automatically.

If you don't use using , then in most cases storing a connection and reusing it is not a good idea (except if you like errors), so the answer would be "as soon as possible".

EDIT (following OP)

This is how I'm opening the connection in App.xaml.cs

  1. You don't want to have anything related to DB connections in the view.
  2. With the samples you provided, I don't see any advantage of doing so. You could simply open the connection every time.
  3. If you worry about opening X connections, then take a look at the following MSDN article . It is about SqlConnections but the concept is the same for SQLiteConnections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM