Need to close thread and database?

Put your problem here if it does not fit any of the other categories.

Need to close thread and database?

Postby cabernet1976 » Sat Jan 26, 2008 4:01 am

Do I need to close thread and database in Activity's onDestroy? e.g.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         @Override
  3.  
  4.         public void onDestroy() {
  5.  
  6.                 myThread.interrupt();
  7.  
  8.                 myDB.close();
  9.  
  10.                
  11.  
  12.                 super.onDestroy();
  13.  
  14.         }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


Or could you give me a suggestion about the thread and database?
Thank you.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Top

Postby cabernet1976 » Thu Jan 31, 2008 2:54 pm

My program often gets the following log in LogCat:
----
D/dalvikvm(744): Exception java/lang/IllegalStateException from SQLiteCursor.java:384 not caught locally
I/dalvikvm(744): Uncaught exception thrown by finalizer (will be discarded):
I/dalvikvm(744): java/lang/IllegalStateException: Finalizing cursor android.database.sqlite.SQLiteCursor@4004d370 on t_Accounts that has not been deactivated or closed
I/dalvikvm(744): at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:384)
I/dalvikvm(744): at android.dalvik.NativeStart.run(Native Method)
----

I wonder what happen, but the program runs fine.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby cabernet1976 » Fri Feb 01, 2008 2:21 am

Could somebody help me or give me some suggestions :?:
Thank you :!:
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby res » Fri Feb 01, 2008 3:42 am

I would close the database in the finally block after you have opened it. Thats how I've seen in tutorials thus far.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. try
  2.  
  3. {
  4.  
  5.     open database
  6.  
  7.     ....
  8.  
  9.     execute SQL
  10.  
  11. } catch (Exception e) {}
  12.  
  13. finally {
  14.  
  15.    if database != null
  16.  
  17.       close database
  18.  
  19. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Postby cabernet1976 » Fri Feb 01, 2008 5:41 am

Hi res,

Thanks for your reply.

I think I should refine my program, the current implement of mine is: open database in activity's onCreate (or class' constructor) and operate database's tables in any place of the class.
I will open database only when I need to operate the tables, and close it at once after completing the operation.

Thank you again.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby res » Fri Feb 01, 2008 5:21 pm

As time goes by we will discover the best ways to use database's in Android, for now though its probably easiest to just follow the tutorials from Google.
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests