I have an Android application with a background service running all the time in the background. When the phone is unlocked, the background service is creating a database. When the phone is locked again, the database is closed. The problem now is that sometimes when the phone is unlocked I'm getting the following message and the app starves. When I'm getting this message then the message appears repeatedly every second.
W/SQLiteConnectionPool: The connection pool for database '+storage+3565-6665+Android+data+com.example+files+Test+database+session_113_db' has been unable to grant a connection to thread 4208 (arch_disk_io_0) with flags 0x2 for 52.066 seconds. Connections: 0 active, 1 idle, 0 available.
In the background service I'm opening the database as follows:
CountDownLatch countDownLatch = new CountDownLatch(1);
String DBName = "session_" + session_nr + ".db";
DatabaseInstance.getInstance().setDB(getApplicationContext(), DBName, countDownLatch);
boolean completed = false;
try {
completed = countDownLatch.await(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
The class DatabaseInstance is as follows:
public class DatabaseInstance {
private Database db;
private volatile boolean open;
private static final DatabaseInstance databaseInstance = new DatabaseInstance();
public static DatabaseInstance getInstance() {
return databaseInstance;
}
private DatabaseInstance() { }
public void setDB(Context context, String DBName, CountDownLatch countDownLatch) {
if ((db != null)) {
db.close();
db = null;
open = false;
}
File f = Storage.joinPath(Storage.getApplicationPath(context), "database", true);
String dbPath = Storage.joinPath(f, DBName, false).getAbsolutePath();
RoomDatabase.Callback rdc = new RoomDatabase.Callback() {
public void onCreate(@NotNull SupportSQLiteDatabase db) {
super.onCreate(db);
}
public void onOpen(@NotNull SupportSQLiteDatabase dbx) {
super.onOpen(dbx);
open = true;
countDownLatch.countDown();
}
};
db = Room.databaseBuilder(context.getApplicationContext(), Database.class, dbPath).addCallback(rdc).build();
}
Does somebody see why this can happen? I'm only using this singleton class to create the database and the database is also always closed before opening another database.