I am working on a chat application and in chat we are getting lots of update from server. And we are also saving the updates into the local SQLite database file.
Can someone suggest me, after every single insertion/update, should we close the database or we should close the database when it is actually needed ?
Closing the connection throws away the page cache, and requires that the schema version is checked and the entire schema is re-parsed the next time it is opened.
In most apps, there are not enough database accesses so that the overhead of continually re-opening the database would actually become noticeable. But this is no reason to add useless code to your app.
Please note that the SQLiteDatabase object is reference counted. So if you are using a global open helper instance, you can keep the DB open with an extra getWritableDatabase() call, even when all your other code calls close().
You should close the connection after each query.
In .net (and in most other frameworks), SQLConnections are stored in the background anyway. It won´t hit your runtime.
Also: connection pooling is your friend.
Related
I have an android application in which I am using Content Provider on top of the database. However, I see that database gets created only when I insert the first record in the database through the Insert of Content Provider. In this scenario, if there's an error in database creation that would remain pending till the insertion of a record.
I would like to know if there's a way I can create the database when the app is accessed for the first time, so, any possible errors in the database creation appear at the earlier stage.
Within our app we show a splash screen specifically for the purpose of creating the db and initialising some application singletons. I'd recommend something similar.
You can start a db (on)Create or db (on)Upgrade by calling SQLiteDatabase#getWritableDatabase or equivalent. Remember to perform this on a background thread and use standard eventing / callbacks to understand when this (synchronous) method has completed.
If something goes wrong then you can catch that exception during start-up although you can't really recover by the sounds of it so you'll be best off not catching any exceptions and making sure your critical init code is bullet proof.
When I look at examples from tutorials I see that for getReadableDatabase the db is not closed at the end but for getWritableDatabase it is always closed at the end. Why is that? Even the docs specify that I must call close on getWritableDatabase. I have read both docs, so please do not simple quote the docs to me. Thanks.
Despite what the documentation implies, there isn't really a relevant difference between getReadableDatabase() and getWritableDatabase(); both open the database, and that database stays open until it is close()d.
So those examples are not correct; they should handle both cases in the same way.
Please note that "when you no longer need the database" does not necessarily mean that you need to close it after each query; if, for example, an activity is likely to access the database multiple times, it is perfectly valid to open it when the activity is started, and to close it when the activity is stopped.
An open database connection reserves some memory for its cache, and the system stops activities if it needs more memory, so you should ensure that there is no open database when there is no active activity.
You need to keep the database open as long as there are cursors in use that contain data from it. I myself rarely close the database, even when using getWriteableDatabase().
As per android documentation sqlite database
Writable database "Once opened successfully, the database is cached, so you can call this method every time you need to write to the database. (Make sure to call close()when you no longer need the database.) Errors such as bad permissions or a full disk may cause this method to fail, but future attempts may succeed if the problem is fixed."
As the writable is cached as soon it's opened it means it will take up memory.
And if database is large then huge memory , so not closing it or keeping it open may lead to member leaks.
Hope this helps you :)
In my scenario it has Sync_Class that syncs, with AsyncTasks in background , from my app to my server.
Every time that my app does one action that need to change data from my SQLite, as first step my app updates my local database as second step throws a AsyncTask in background to start the sync with my server. In the 80% of cases my app works great but the other 20% of cases throws a IllegalStateException because the app try to re-open the connection or open a closed connection, when I have a method to open database in 6 lines more above. In this cases I think the problem is multiple simultaneous acceses in database, I'm right?
In the differents posts that I can read, the people talk about de SQLite can't execute a simultaneous connections and it serialized connections because file structures not permit... The final question is, If we do multiples asyncTasks with sqlite connections to do inserts, updates and deletes, to harness the full power of the processor with parallel programming, we have any tools for do this? Or it's a non-viable option and we need to do a serialized connections?
If you have any solutions or any ideas for my problem, help me!! Thanks in advance!!
More info:
My BDDclass have a method for open database. When I need to execute query, rawquery, etc... in a simple function or void I call my database class, opens database, executes the query or multiple queries and at the end we close the database. When I know that i have a large process with a multiples functions with querys in this case I create BDDclass and opens bdd at start and at the end closes the database of this process.
I say this because I can see some posts that people recommend use the SQLiteHelper because this helps to administrate multiple simultaneous connections in SQLite, but others posts says that have the same problem that I have... Then it's must to use SQLiteHelper? Or not?
If you need more information or something let me know.
Finally I solved the problem!! The problem is that I try to control the acceses to the database, opening and closing the databases when I need read or write in the databases with parallel programming (background asyncTasks).
Must remember that I don't use SQLiteHelper and I solved the problem calling my database class one time per activity, this mean open my database one time and close this when my app is pause and reconect with BDD when my app come in first plane other time then connects with my database.
In resume, never close your database and you can use a parallel programming with multiple accesses in SQLite. Greetings!!
I'm working with SQLite in my android project. There is a feature that is clear to me -- if there are multiple threads working with DB then they should use only ONE instance of DBHelper and SQLite guarantee a safe access (even if it is concurrent) to the DB.
But I still have one thing needed to be clarified. How should I manage with DB connection (SQLiteDatabase object)? How often should I call geWritableDatabase() and close()? Is it ok if I call these methods once? Or it's better to obtain SQLDatabase object and close one every time I perform read/write operation on DB?
It is handled by default... I mean if a writtable database open, whenever you try to access read-only database it closes writtable one and create new read-only database instance. As far as i know :)
We all learn that resources, such as database connections, should be acquired late and released early.
Yet applying this principle to SQLite database connections on Android have caused me some headache.
I have an app that download updates from a backend server in a service working in the background, writing updates to the database at a regular basis. The problem I experience arise when the following sequence occurs:
Service opens a writable database connection
Some activity opens a readable database connection
Service closes its database connection concurrently with the activity reading data
Activity fails due to its database connection was closed
Both the service and the activity uses the same SQLiteOpenHelper class, though different instances, to open their connections. My initial assumption was that this should work just fine, but somehow it seems that the underlying connection is shared between the two database instances.
To work around the problem I ended up not closing the database objects, only closing any opened cursors. This seems to work, though I'm not sure that I'm not leaking memory here.
Is there something obvious I am missing here?
Is there something obvious I am missing here?
I'd say no. Looking at the source code to SQLiteOpenHelper, I can't see how two instances could be sharing a SQLiteDatabase object.
Some diagnostic suggestions:
Dump the toString() value of each SQLiteDatabase, which should give you a Java instance ID. If they are the same, that is where your problem lies, and you will need to work your way upstream to figure out how the heck this is happening (e.g., you really are using the same instance of the SQLiteOpenHelper).
With your database in a stable state (i.e., no need to create or upgrade), flip one of your two database spots to use SQLiteDatabase directly, rather than via SQLiteOpenHelper, and see if that changes matters.