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();
}