Room Android: Unable to grant a connection to thread (Connection Pool for database)

I have the following code using Room library to handle sqldatabase. I waSthe downloadCustomers method many times but all of a sudden i got the following message inside at the mDatabase.repoCustomer().deleteCustomers() :

Message:

The connection pool for database has been unable to grant a connection to thread room android

This happened only once and i wiped the emulator so i can run it again.
I read in forums that “a transaction might be open by a thread”, but Room library handles automatically situations like these.

Any ideas?

Emulator: Emulator API 27 with Google Play (not Google APIs)

Something to consider: Sometimes when i run apps with Google play strange things happen e.x runs an app with 2 instances at the same time. But i dont want to make quick assumptions.

Thank you.

public abstract class AppDatabase extends RoomDatabase {

private static AppDatabase sInstance;

@VisibleForTesting
public static final String DATABASE_NAME = "Database_db";

public abstract CustomerDao repoCustomer();


public static AppDatabase getInstance(Context context) {

    if (sInstance == null) {
        synchronized (AppDatabase.class) {
            if (sInstance == null) {
                sInstance = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DATABASE_NAME).build();
             
            }
        }
    }
    return sInstance;
}


public void downloadCustomers(final String table){

        executors.diskIO().execute(new Runnable() {
            @Override
            public void run() {
                mDatabase.repoCustomer().deleteCustomers();

                if (mDatabase.repoCustomer().getAllCustomers().size() == 0) {
                    makeHttpRequest();
                 }
             });
        }  
}


@Dao
public interface CustomerDao {

    @Transaction @Query("SELECT * FROM Customer")
    LiveData<List<Customer>> getAllCustomersLive();

    @Transaction @Query("SELECT * FROM Customer")
    List<Customer> getAllCustomers();

    @Transaction @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertCustomers(List<Customer> Customers);

    @Query("DELETE FROM Customer")
    int deleteCustomers();
}

Not a direct answer, but I thinking about possible issues, you may check.

This could be happened if you grab a connection, and you don’t close it. And once pool thinks there is no connection left to give, it would throw the exception.

If the connection/resource you get implements closeable, so simply use the connection with try, liek following

try(Connection conn=...){
//use conn...
}//conn will be closed automatically

Another possible issue could be threading. Make sure you create the thread with correct context. For instance you may not change any UI stuff with a thread created outside of the UI context. Instead you should ask the UI context to run the thread with its context for you.

Maybe for some situation, and some state your application act and do some specific operation that you face with it

i wish I could help you more, but I ain’t a Android dev mate, but I believe you can fix it.

@NULL_dev “This could be happened if you grab a connection, and you don’t close it. And once pool thinks there is no connection left to give, it would throw the exception.” Room handles this automatically.