Google Groups

Re: [android-developers] Re: Where do we have to close the DB in a ContentProvider?


Dianne Hackborn Aug 21, 2009 9:56 AM
Posted in group: Android Developers
A content provider is created when its hosting process is created, and remains around for as long as the process does, so there is no need to close the database -- it will get closed as part of the kernel cleaning up the process's resources when the process is killed.

On Fri, Aug 21, 2009 at 2:02 AM, S. Mccoy <mccoy....@gmail.com> wrote:

Hi,

 With an Activity there is no problem, the lifecycle is clear and we
have to realease all the resources in that method. The problem is that
I use this helper inside a ContentProvider. In this case there is
neither clear lifecycle (almost for me) nor a “close()” or “onDestroy
()” method where release the resources.

S.


On 21 ago, 09:54, jiaoni <jiaoni5...@gmail.com> wrote:
> Hi S,
>
> I use a DBHelper class which extends the SQLiteOpenHelper class, and I
> close it in the onDestroy() function of the Activity class.
>
> Jiaoni
>
> On Aug 21, 3:15 pm, "S. Mccoy" <mccoy.sher...@gmail.com> wrote:
>
> > Hi all,
>
> > I have seen that if we use an AbstractSyncableContentProvider (other
> > providers like ContactProvider use it), a “close()” method is
> > implemented:
> > public void close(){
> >     if(mOpenHelper != null)
> >         mOpenHelper.close();}
>
> > In this case, the helper seems to be closed (and also de DB), the same
> > helper that remains open in a ContentProvider. I can’t understand the
> > lifecycle of the ContentProvider without a method to release all the
> > things. Any ideas ?.-Thanks in advance.
>
> > S.
>
> > On 18 ago, 18:00, "S. Mccoy" <mccoy.sher...@gmail.com> wrote:
>
> > > Maybe it’s an easy question, but I’ve tried to understand why all the
> > > content providers seem not to close the DB that they use. In the
> > > NotePadProvider example (in other providers is similar), it’s used a
> > > Helper to manage the DB (that extends of SQLiteOpenHelper). This
> > > helper has a method to close the DB (http://developer.android.com/
> > > reference/android/database/sqlite/SQLiteOpenHelper.html#close%28%29),
> > > but in all the providers that I have seen (even in the example), this
> > > method is never used.
>
> > > For example, in the query method of the NotPadProvider we use the
> > > helper to open a DB
>
> > > // Get the database and run the query         SQLiteDatabase db =
> > > mOpenHelper.getReadableDatabase();         Cursor c = qb.query(db,
> > > projection, selection, selectionArgs, null, null,
> > > orderBy);          // Tell the cursor what uri to watch, so it knows
> > > when its source data changes         c.setNotificationUri(getContext
> > > ().getContentResolver(), uri);         return c;
>
> > > the mOpenHelper is create in the “onCreate()” method, but there is no
> > > reference to a mOpenHelper.close() in all the provider (maybe because
> > > there is no “close()” method in ContentProvider).
>
> > > Do we need to close the SQLiteOpenHelper object? When we use it inside
> > > a ContentProvider, where is the right place to close it?
>
> > > Thanks in advance.
> > > S.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.