From: Damyan Ivanov Date: Sun, 24 Mar 2019 17:11:48 +0000 (+0200) Subject: drop 'synchronized' modifier for getDatabase X-Git-Tag: v0.8.1~11 X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=commitdiff_plain;h=c63db65ac315e371325031c90b098bab48661e2d drop 'synchronized' modifier for getDatabase it was necessary to guarantee that no two connections were created because the connection is created in getDatabase by moving the connection creatin to the init() a high profile method (getDatabase) gets faster. init() is called only once anyway --- diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java b/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java index a7e124d7..a7389563 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java @@ -58,12 +58,11 @@ public final class MLDB { if (context == null) throw new IllegalStateException("First call init with a valid context"); } - public static synchronized SQLiteDatabase getDatabase() { + public static SQLiteDatabase getDatabase() { checkState(); SQLiteDatabase db; - if (dbHelper == null) dbHelper = new MobileLedgerDatabase(context); db = dbHelper.getWritableDatabase(); db.execSQL("pragma case_sensitive_like=ON;"); @@ -196,13 +195,16 @@ public final class MLDB { }); } } - public static void init(Application context) { + public static synchronized void init(Application context) { MLDB.context = context; + if (dbHelper != null) throw new IllegalStateException("It appears init() was already called"); + dbHelper = new MobileLedgerDatabase(context); } - public static void done() { + public static synchronized void done() { if (dbHelper != null) { Log.d("db", "Closing DB helper"); dbHelper.close(); + dbHelper = null; } } }