title: Using room as singleton in kotlin
body: <p>I'm trying to use Room as singleton so I didn't have to invoke <code>Room.databaseBuilder()</code> -which is expensive- more than once.</p>
  
  <pre><code>@Database(entities = arrayOf(
          Price::class,
          StationOrder::class,
          TicketPrice::class,
          Train::class,
          TrainCategory::class
  ), version = 2)
  @TypeConverters(Converters::class)
  abstract class AppDatabase : RoomDatabase() {
  
      abstract fun dao(): TrainDao
  
  companion object {
          fun createDatabase(context: Context): AppDatabase
                  = Room.databaseBuilder(context, AppDatabase::class.java, "trains.db").build()
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Note:</p>
  
  <ol>
  <li>Can't use Object because Room requires using <code>abstract class</code>.</li>
  <li>singleton must be thread safe because multiple threads might access it at the same time.</li>
  <li>must be able to take <code>Context</code> as an argument.</li>
  </ol>
  
  <hr>
  
  <p>I have looked at all similar StackOverflow questions and none of them satisfy my requirements</p>
  
  <p><a href="https://stackoverflow.com/questions/33128024/singleton-with-argument-in-kotlin">Singleton with argument in Kotlin</a> <strong>isn't thread-safe</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/35634803/kotlin-best-way-to-convert-singleton-databasecontroller-in-android">Kotlin - Best way to convert Singleton DatabaseController in Android</a> <strong>isn't thread-safe</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/35587652/kotlin-thread-save-native-lazy-singleton-with-parameter">Kotlin thread save native lazy singleton with parameter</a> <strong>uses object</strong></p>