Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: opening and closing SQLite database
    primarykey
    data
    text
    <p>I'm developing and android application in which I often use access the local database. This database can be accessed from differents therads, so I have a coordination problem for the database. I use the following <code>open()</code> and <code>close()</code> method.</p> <pre><code>public void open(){ mDb=mDbHelper.getWritableDatabase(); } public void close(){ mDb.close(); } </code></pre> <p>So, usually, when I need to access the db for some operations I open the database, then I perform some operation, and finally I close the database. The code I typically use for this purpose is the following:</p> <pre><code> try { dbManager.open(); // database operation } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { dbManager.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } </code></pre> <p>But, if for this piece of code is used from differnts threads (supposing thread A and thread B) the following situation may occurs:</p> <pre><code>A thread: performs open() B thread: perfroms open() A thread: perfroms some operation A thread: performs close() B thread: try to perform some operation but it fails! </code></pre> <p>So, the only solution I can guess I to perform <code>open()</code> when my application starts and <code>close()</code> when my application is stopped. I'm not sure that this can be this a good solution? </p> <p>In effect, the documentation of <code>getWritableDatabase()</code> method (called from my <code>open()</code>) says:</p> <pre><code>Make sure to call close() when you no longer need the database </code></pre> <p>So, anyone can suggest me an alternative solution?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload