I am working on some code where I create a HikariDataSource as a by lazy value. I did apply the mode LazyThreadSafetyMode.SYNCHRONIZED, so I am confused as to why the initialization is done twice when I spin up a new thread that reads that value
// in module database
val dataSource: HikariDataSource by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
HikariDataSource(dataSourceConfig(databaseConfig))
}
fun initDatabase() {
dataSource.connect().let { //do stuff}
}
// in main module
fun main() {
initDatabase()
thread {
dataSource.connect().let { // initializes a new data source... }
}
}
I would expect the HikariDataSource to be initialized once, but for some reason its called twice... I think the fact that I am exposing that variable to another gradle project might affect it, but not sure why..