Merge "RoomDataBaseBuilder.build() should be safe to call multiple times. The current implementation double wraps query callbacks." into androidx-master-dev
diff --git a/room/integration-tests/kotlintestapp/src/androidTest/java/androidx/room/integration/kotlintestapp/test/QueryInterceptorTest.kt b/room/integration-tests/kotlintestapp/src/androidTest/java/androidx/room/integration/kotlintestapp/test/QueryInterceptorTest.kt
index a23ce6b..8ad0e02 100644
--- a/room/integration-tests/kotlintestapp/src/androidTest/java/androidx/room/integration/kotlintestapp/test/QueryInterceptorTest.kt
+++ b/room/integration-tests/kotlintestapp/src/androidTest/java/androidx/room/integration/kotlintestapp/test/QueryInterceptorTest.kt
@@ -182,6 +182,36 @@
         )
     }
 
+    @Test
+    fun testCallbackCalledOnceAfterCloseAndReOpen() {
+        val dbBuilder = Room.inMemoryDatabaseBuilder(
+            ApplicationProvider.getApplicationContext(),
+            QueryInterceptorTestDatabase::class.java
+        ).setQueryCallback(
+            RoomDatabase.QueryCallback { sqlQuery, bindArgs ->
+                val argTrace = ArrayList<Any>()
+                argTrace.addAll(bindArgs)
+                queryAndArgs.add(Pair(sqlQuery, argTrace))
+            },
+            MoreExecutors.directExecutor()
+        )
+
+        dbBuilder.build().close()
+
+        mDatabase = dbBuilder.build()
+
+        mDatabase.queryInterceptorDao().insert(
+            QueryInterceptorEntity("Insert", "Inserted a placeholder query")
+        )
+
+        assertQueryLogged(
+            "INSERT OR ABORT INTO `queryInterceptorTestDatabase` (`id`,`description`) " +
+                "VALUES (?,?)",
+            listOf("Insert", "Inserted a placeholder query")
+        )
+        assertTransactionQueries()
+    }
+
     private fun assertQueryLogged(
         query: String,
         expectedArgs: List<String?>
diff --git a/room/runtime/src/main/java/androidx/room/RoomDatabase.java b/room/runtime/src/main/java/androidx/room/RoomDatabase.java
index 76e6a8f..4561876 100644
--- a/room/runtime/src/main/java/androidx/room/RoomDatabase.java
+++ b/room/runtime/src/main/java/androidx/room/RoomDatabase.java
@@ -1172,8 +1172,12 @@
                 }
             }
 
+            SupportSQLiteOpenHelper.Factory factory;
+
             if (mFactory == null) {
-                mFactory = new FrameworkSQLiteOpenHelperFactory();
+                factory = new FrameworkSQLiteOpenHelperFactory();
+            } else {
+                factory = mFactory;
             }
 
             if (mCopyFromAssetPath != null
@@ -1193,12 +1197,12 @@
                             + "Builder, but the database can only be created using one of the "
                             + "three configurations.");
                 }
-                mFactory = new SQLiteCopyOpenHelperFactory(mCopyFromAssetPath, mCopyFromFile,
-                        mCopyFromInputStream, mFactory);
+                factory = new SQLiteCopyOpenHelperFactory(mCopyFromAssetPath, mCopyFromFile,
+                        mCopyFromInputStream, factory);
             }
 
             if (mQueryCallback != null) {
-                mFactory = new QueryInterceptorOpenHelperFactory(mFactory, mQueryCallback,
+                factory = new QueryInterceptorOpenHelperFactory(factory, mQueryCallback,
                         mQueryCallbackExecutor);
             }
 
@@ -1206,7 +1210,7 @@
                     new DatabaseConfiguration(
                             mContext,
                             mName,
-                            mFactory,
+                            factory,
                             mMigrationContainer,
                             mCallbacks,
                             mAllowMainThreadQueries,