Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
Posts: 178
Registered: ‎02-20-2011
My Device: 8700
My Carrier: at&t

Thread Safe DBHelper

My DBHelper always block the UI thread, finally I got some logs when got stucked:

   public void print()
   {
	   Log.d("DBHelper", "getInstance(): 3.1 ~~~~~~~~~~~~~~~~~~~~~~~");
	   Log.i(this, "print()");
	   
	   Statement s = null;
	   Cursor c = null;
	   
	   try {
		   Log.d("DBHelper", "getInstance(): 3.2 ~~~~~~~~~~~~~~~~~~~~~~~");
		   open();
		   Log.d("DBHelper", "getInstance(): 3.3 ~~~~~~~~~~~~~~~~~~~~~~~");
		   s = _db.createStatement("SELECT * FROM _download_table");
		   Log.d("DBHelper", "getInstance(): 3.4 ~~~~~~~~~~~~~~~~~~~~~~~");
		   s.prepare();
		   Log.d("DBHelper", "getInstance(): 3.5 ~~~~~~~~~~~~~~~~~~~~~~~");
		   
		   c = s.getCursor();
		   Log.d("DBHelper", "getInstance(): 3.6 ~~~~~~~~~~~~~~~~~~~~~~~");
		   Row r = null;
		   Log.d("DBHelper", "getInstance(): 3.7 ~~~~~~~~~~~~~~~~~~~~~~~");
		   
		   while(c.next()) {
			   Log.d("DBHelper", "getInstance(): 3.8 ~~~~~~~~~~~~~~~~~~~~~~~");
			   r = c.getRow();
			   Log.d("DBHelper", "getInstance(): 3.9 ~~~~~~~~~~~~~~~~~~~~~~~");
			   Log.i(this, "print(): _id = " + r.getInteger(_INDEX_ID) + ", _sku = " + r.getString(_INDEX_SKU) + ", _name = " + r.getString(_INDEX_NAME) + ", _percent = " + r.getInteger(_INDEX_PERCENT) + ", _file_size = " + r.getInteger(_INDEX_FILE_SIZE) + ", _total_size = " + r.getInteger(_INDEX_TOTAL_SIZE));
		   } 
		   
		   Log.d("DBHelper", "getInstance(): 3.10 ~~~~~~~~~~~~~~~~~~~~~~~");
//		   c.close();
		   Log.d("DBHelper", "getInstance(): 3.11 ~~~~~~~~~~~~~~~~~~~~~~~");
//		   close();
		   Log.d("DBHelper", "getInstance(): 3.12 ~~~~~~~~~~~~~~~~~~~~~~~");
	   } catch (Exception e) {
		   Log.d(this, "print(): Exception = " + e.getMessage());
		   IOUtil.showAlert(getClass().getName() + ".print(): Exception = " + e.getMessage());
		   Log.d("DBHelper", "getInstance(): 3.13 ~~~~~~~~~~~~~~~~~~~~~~~");
	   } finally {
		   try {
			   Log.d("DBHelper", "getInstance(): 3.14 ~~~~~~~~~~~~~~~~~~~~~~~");
			   if(c != null)
				   c.close();
			   Log.d("DBHelper", "getInstance(): 3.15 ~~~~~~~~~~~~~~~~~~~~~~~");
			   close();
			   Log.d("DBHelper", "getInstance(): 3.16 ~~~~~~~~~~~~~~~~~~~~~~~");
		   } catch (DatabaseException e) {
			   e.printStackTrace();
		   }
		   Log.d("DBHelper", "getInstance(): 3.17 ~~~~~~~~~~~~~~~~~~~~~~~");
	   }
	   Log.d("DBHelper", "getInstance(): 3.18 ~~~~~~~~~~~~~~~~~~~~~~~");
   }

 It seems two threads are operating print(), and we can see it get stucked at c.close():

[0.0] <!> DBHelper	getInstance(): 3.1 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] (i) DBHelper	print()
[0.0] <!> DBHelper	getInstance(): 3.2 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] (i) DBHelper	open()
[0.0] <!> DBHelper	getInstance(): 3.3 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.4 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.5 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.6 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.7 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.5 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.6 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.7 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.8 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.9 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.8 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.9 ~~~~~~~~~~~~~~~~~~~~~~~

[0.0] <!> DBHelper	getInstance(): 3.8 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.9 ~~~~~~~~~~~~~~~~~~~~~~~

[0.0] <!> DBHelper	getInstance(): 3.8 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.9 ~~~~~~~~~~~~~~~~~~~~~~~

[0.0] <!> DBHelper	getInstance(): 3.10 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.11 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.12 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.14 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.15 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] (i) DBHelper	close()
[0.0] <!> DBHelper	getInstance(): 3.10 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.11 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.12 ~~~~~~~~~~~~~~~~~~~~~~~
[0.0] <!> DBHelper	getInstance(): 3.14 ~~~~~~~~~~~~~~~~~~~~~~~

 

 Anyone have any suggestions? thanks in advance!

 

# I tried to change the world, but I couldn't find the source code #



Highlighted
Developer
Posts: 246
Registered: ‎12-13-2010
My Device: Not Specified

Re: Thread Safe DBHelper

I think the full code listing is necessary.