SQLite database, multithreading, Locks and account sync on android SQLite database, multithreading, Locks and account sync on android sqlite sqlite

SQLite database, multithreading, Locks and account sync on android


For multiple threads accessing, it is advisable to use the singleton pattern.

Such a way, successive calls to the same database will be seamlessly serialised.

However, it's not impossible to have some NullPointerExceptions on inserts. So, to expand your "Thread.sleep" logic, you could use this code:

@Overridepublic SQLiteDatabase getWritableDatabase() {    while (true) {        try {            return super.getWritableDatabase();        } catch (SQLiteDatabaseLockedException e) {            System.err.println(e);        }        try {            Thread.sleep(500);        } catch (InterruptedException e) {            System.err.println(e);        }    }}