09-11-2013 09:51 AM
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
09-12-2013 01:46 AM
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.
09-12-2013 04:37 AM
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.
09-12-2013 09:01 AM
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.
09-12-2013 09:56 AM
closing and opening db is the best practice if you are not using it frequently..As peter replied..
10-02-2013 09:46 AM
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)
10-02-2013 10:24 AM
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.