java.lang.IllegalStateException: attempt to re-open an already-closed object: or Attempt to reopen an already-closed object sqlitedatabase or Room attempt to re-open an already closed database.

Problem:-

The problem is that you are doing a long or large Database operation in Database than that connect frequently to the database.

My Case problem:-

I was getting java.lang.IllegalStateException: attempt to re-open an already-closed object:---
in this code below. I also Insert Data and creating Table all at the same time.

Problem code:-
private void create_database() {
    Thread thread = new Thread(() -> {
        main_db = 
              openOrCreateDatabase(ClsGlobal.Database_Name, 
                MODE_PRIVATE, 
                             null);
        // In this createCityMaster i am also inserting city 
        //name's.
        createCityMaster();
    // In this createStateMasteri am also inserting State 
      //    name's.
        createStateMaster();
        createSizeMaster();
        createUnitMaster();
        createCustomerMaster();
        createEmailLogs();
        createInventoryDetail();
        createInventoryMaster();

        createPaymentMaster();
        createPurchaseMaster();
        createPurchaseDetail();
        createCommonLogsMaster();
        createOrder_Sequence();

        if (main_db.isOpen()){

            main_db.close();
        }
    });

    thread.start();


}

Solution:-

1) Use Transaction for long Database operation.

    Solution code:-
private void create_database() {
    Thread thread = new Thread(() -> {
        main_db = 
              openOrCreateDatabase(ClsGlobal.Database_Name, 
                MODE_PRIVATE, 
                             null);

          main_db.beginTransaction();

        // In this createCityMaster i am also inserting city 
        //name's.
        createCityMaster();
    // In this createStateMasteri am also inserting State 
      //    name's.
        createStateMaster();
        createSizeMaster();
        createUnitMaster();
        createCustomerMaster();
        createEmailLogs();
        createInventoryDetail();
        createInventoryMaster();

        createPaymentMaster();
        createPurchaseMaster();
        createPurchaseDetail();
        createCommonLogsMaster();
        createOrder_Sequence();

        if (main_db.isOpen()){
            main_db.setTransactionSuccessful();
            main_db.endTransaction();
            main_db.close();
        }
    });

    thread.start();


}

Reference:-
1) https://medium.com/@JasonWyatt/squeezing-performance-from-sqlite-insertions-971aff98eef2


//------------------------------------------- End ------------------------------------------------//

Result:-

java.lang.illegalstateexception attempt to reopen an already-closed object sqlitedatabase

attempt to re-open an already-closed object: sqlitequery:

android room migration

room illegalstateexception attempt to re open an already closed object

drop room database

room database migration kotlin

room database fallback to destructive migration

room migration medium

attempt to re open an already closed object sqlitedatabase android room

attempt to re-open an already-closed object sqlitedatabase room

attempt to re-open an already-closed object room

attempt to re-open an already-closed object sqlitedatabase migration

android room attempt to re-open an already-closed object

attempt to re-open an already-closed object room migration

java lang illegalstateexception room cannot verify the data integrity

room migration medium

attempt to re open an already closed object sqlitedatabase android room

attempt to re-open an already-closed object room

attempt to re-open an already-closed object sqlitedatabase room

android room migration add column

android room migration add table

android room migration didn't properly handle

android room migration drop table

android room migration rename column

room migration example

android room database migration

android room migration create table

attempt to re-open an already-closed object: sqlitequery:

room illegalstateexception attempt to re open an already closed object

android room migration

android room rename column

migrate to room

room migration medium

drop room database

room allow destructive migration





Comments

Popular posts from this blog

Android Studio 3.5 or above usb device not detected in Android Studio or Unable to detect adb version, exit value: 0xc0000135. ADB depends on the Windows Universal C Runtime, which is usually installed by default via Windows Update. You may need to manually fetch and install the runtime package here: https://support.microsoft.com/en-ca/help/2999226/update-for-universal-c-runtime-in-windows

Lint found fatal errors while assembling a release target.

ViewModelProviders.of(this) is deprecated what to use instead ViewModelProviders.of(this) OR if you don't want to use ViewmodelFactory OR ViewModelProviders error.