Using a singleton to open the database, where/when would you close the database? Should the singleton keep track of open connections from clients (the singleton would expose a close method) and close it when all "connections" are closed or should Application be sub-classed and the db closed in onDestroy()?
Emanuel Moecklin On Apr 7, 2:22 pm, Dianne Hackborn <[email protected]> wrote: > Don't do that. Only open the database once. If you are not using a content > provider, implement a singleton that takes care of opening the database once > for all code in your app. > > And let me be clear -- there is NOTHING wrong with the content provider > never closing the database. Nothing. That is NO reason to avoid using a > content provider. In fact it is a very good design approach, and if you > aren't using a content provider but directly accessing, I would recommend > using the exact same approach for the singleton implementation. > > If you want to have a content provider *AND* also elsewhere do direct access > to the database, then implement your content provider on top of the > singleton that owns the database. > > > > > > > > > > On Thu, Apr 7, 2011 at 10:39 AM, Nathan <[email protected]> wrote: > > > On Apr 7, 9:23 am, Dianne Hackborn <[email protected]> wrote: > > > > You should never open a database multiple times. > > > Let me make sure I understand this rule. > > > It seems reasonable to pass an intent: > > + A path to a database > > + A key to find information in said database. > > > Then the activity will create a list, form etc, after creating an > > SQLiteDatabase and then a cursor. It will then make sure that it > > closes the cursor and close the SQliteDatabase at the appropriate spot > > in its lifecycle. > > > But if you have multiple activities, this would violate the rule about > > opening a database multiple times. > > > You would pretty much have to have a static factory that keeps track > > of open databases in a hashtable, keyed by filename, with reference > > counting. Am I right? > > > As has been noted in these forums, a ContentProvider *never* closes a > > database because is there is no call on its interface to do so. I > > haven't been using contentproviders but this seems bad, and will > > definitely leave red messages in your log. > > > Nathan > > > -- > > You received this message because you are subscribed to the Google > > Groups "Android Developers" group. > > To post to this group, send email to [email protected] > > To unsubscribe from this group, send email to > > [email protected] > > For more options, visit this group at > >http://groups.google.com/group/android-developers?hl=en > > -- > Dianne Hackborn > Android framework engineer > [email protected] > > 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. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

