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
Developer
Posts: 326
Registered: ‎02-02-2009
My Device: Not Specified

SQLite DB open once or open when required

Hi

 

My application works in background too , and using sqlite for storage

my question is which is the best practice

1. Open the DB once and pass it to all method (please note my application works in background)

2. Open the DB when required and close after use

 

 

Highlighted
Regular Contributor
Posts: 66
Registered: ‎08-28-2010
My Device: 9630

Re: SQLite DB open once or open when required

Hi,

 

 Opening DB once is good for practice.  As opening and close DB will create the issue in feature and require time to also affect in performance.

 

 As your applicaiton is going to run all time in Background if you keep DB open it will not create any problem for you.

 

Regards,


 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: SQLite DB open once or open when required

Unfortunately I think the correct answer is - it depends.

 

If you use the DB infrequently (say once every 5 minutes) and you are not performance constrained, then I would definitely close it.  If you use it frequently (say every 10 seconds) or you are really concerned about performance, then leave it open. 

 

Personally I would close it when you are going to be come inactive.  Then you can be sure there is nothing left hanging if the device is unexpectedly shutdown. 

Regular Contributor
Posts: 53
Registered: ‎08-27-2013
My Device: none
My Carrier: none

Re: SQLite DB open once or open when required

It also seems that you need to close and reopen the database if there are more threads dealing with it, the system doesn't seem to like it if more than one thread touches the same database connection and will lock it. In that case you will need to make all methods that are accessing the database synchronized.

Developer
Posts: 151
Registered: ‎09-28-2012
My Device: Blackberry Developer
My Carrier: Aircel

Re: SQLite DB open once or open when required

closing and opening db is the best practice if you are not using it frequently..As peter replied..


----------------------------------------------------------
Feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
Developer
Posts: 326
Registered: ‎02-02-2009
My Device: Not Specified

Re: SQLite DB open once or open when required

Hi All

 

Actually my application runs in background and multiple threads accessa the DB at the same time

so opning and closing result in exceptions and error

so I will have to choce open once method, in this case when to close the db (because my application runs in background, and I dont know whether db operations are done)

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: SQLite DB open once or open when required

That seems like a real problem, so leaving it open does look like the safest option. 

 

I would recommend that you implement a SystemListener (look at the API documentation for this) so that you close the database when the device is powered down.

 

In addition, you could implement some processing to check for the existence of your foreground application and/or have your foreground application actually inform the background processing that the database can be closed as part of its shutdown.