android - Attempt to reopen an already-closed object -


I have three dataset classes for a database assistant class and three tables in the same database, through many async Tasks Databases are used in places. I had to face an "attempt to reopen closed objects ..." problem ... Problem, I searched around and found that dbhelper.getReadableDatabase () was already opened Returns the same object for the connection. I have guessed that when two threads operate simultaneously and one of them finishes its work and calls close () then the connection stops and the thread that flows throws this exception is. Therefore, to avoid off () I have written the following two methods:

  public static synchronized zero newOpenRequest () {requestsOpen ++; Util.debuglog (TAG, "Open Request:" + Request Open); } Public stable synchronized boolean canClose () {requestsOpen--; Util.debuglog (TAG, "Open Request:" + Request Open); If (requestsOpen == 0) return true; return false; }   

In all three data source classes, when I do it in the following way:

  Private zero openRead () {database = dbhelper.getReadableDatabase ( ); DBHelper.newOpenRequest (); Logs. I (tag, "database opened."); } Private Zero openWrite () {database = dbhelper.getWritableDatabase (); DBHelper.newOpenRequest (); Logs. I (tag, "database opened."); } Close private zeros () (If (kanaklos () on dBeach) {dbhelper.close (); Util.debuglog (tag, "database closure.");}}   

My logcat output is as follows:

screen grab of partial logc output

Then in the form of high rectangle in black rectangle, the total openRequests was 0, hence the database closed, normal but as highlighted in the red rectangle, firstly OpenRequests were 0, so at that time only the database was closed Should have been, but (my guess) that happened canClose () came true for a thread, and before calling Code> open () (because openRequests = 1 is on the logcat before shutting down) and then the first code of the closed () is causing another thread to be flawed.

I did not have any problem with this concurrent access problem. Databases never stop in Android Or is it. Then you may not close your fixed DB. It does not matter, open it throughout your app's life.

You do not need to synchronize your database calls when your application ID is lost

< P> DBOpenHelper works just fine:

  public class DBOpenHelper Extended SQLiteOpenHelper {Personal Static FINAL END DATABASE_VERSION = 31; Private stable DBOpenHelper mInstance; Private static final string DATABASE_NAME = "thedb.db"; Public stable DBOpenHelper getInstance (Reference Reference) {if (mInstance == faucet) {mInstance = New DBOpenHelper (context.getApplicationContext ()); } Return M Instance; } Private DBOpenHelper (reference reference) {super (reference, DATABASE_NAME, blank, DATABASE_VERSION); }}   

Sample using DB assistant - close the cursor, but do not have DB

  SQLiteDatabase db = DBOpenHelper.getInstance (context) .getWritableDatabase ( ); Cursor cursor = null; {Cursor = db.query ...} Try {cursor.close () at the end; }    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -