Skip to content

Commit eede932

Browse files
committed
Make dataSource lazy to avoid initialization before needed
Since we now start the applications when generating typescript we need to be able to build the component registry without connecting to a database. This patch moves the actual database connecting to `DBMigrator` and make the `dataSource` member lazy so it isn't accessed before needed.
1 parent 17e22de commit eede932

File tree

11 files changed

+11
-20
lines changed

11 files changed

+11
-20
lines changed

article-api/src/main/scala/no/ndla/articleapi/ComponentRegistry.scala

+1-3
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ class ComponentRegistry(properties: ArticleApiProperties)
8383
)
8484
override val DBUtil: DBUtility = new DBUtility
8585

86-
override val dataSource: HikariDataSource = DataSource.getHikariDataSource
87-
DataSource.connectToDatabase()
88-
86+
lazy val dataSource: HikariDataSource = DataSource.getHikariDataSource
8987
lazy val internController = new InternController
9088
lazy val articleControllerV2 = new ArticleControllerV2
9189
lazy val healthController: TapirHealthController = new TapirHealthController

article-api/src/test/scala/no/ndla/articleapi/repository/ArticleRepositoryTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ArticleRepositoryTest
4141

4242
override def beforeAll(): Unit = {
4343
super.beforeAll()
44-
ConnectionPool.singleton(new DataSourceConnectionPool(dataSource))
44+
DataSource.connectToDatabase()
4545
if (serverIsListening) {
4646
migrator.migrate()
4747
}

audio-api/src/main/scala/no/ndla/audioapi/ComponentRegistry.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ class ComponentRegistry(properties: AudioApiProperties)
6666
new V5__AddAgreementToAudio,
6767
new V6__TranslateUntranslatedAuthors
6868
)
69-
override val dataSource: HikariDataSource = DataSource.getHikariDataSource
70-
DataSource.connectToDatabase()
69+
override lazy val dataSource: HikariDataSource = DataSource.getHikariDataSource
7170

7271
lazy val s3Client = new NdlaS3Client(props.StorageName, props.StorageRegion)
7372
lazy val s3TranscribeClient = new NdlaS3Client(props.TranscribeStorageName, props.TranscribeStorageRegion)

audio-api/src/main/scala/no/ndla/audioapi/repository/AudioRepository.scala

-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ trait AudioRepository {
2424
val audioRepository: AudioRepository
2525

2626
class AudioRepository extends StrictLogging with Repository[AudioMetaInformation] {
27-
ConnectionPool.singleton(new DataSourceConnectionPool(dataSource))
28-
2927
def audioCount(implicit session: DBSession = ReadOnlyAutoSession): Long =
3028
sql"select count(*) from ${AudioMetaInformation.table}"
3129
.map(rs => rs.long("count"))

concept-api/src/main/scala/no/ndla/conceptapi/ComponentRegistry.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ class ComponentRegistry(properties: ConceptApiProperties)
6767
new V25__SubjectNameAsTagsPublished(props)
6868
)
6969

70-
override val dataSource: HikariDataSource = DataSource.getHikariDataSource
71-
DataSource.connectToDatabase()
70+
override lazy val dataSource: HikariDataSource = DataSource.getHikariDataSource
7271

7372
lazy val draftConceptRepository = new DraftConceptRepository
7473
lazy val publishedConceptRepository = new PublishedConceptRepository

database/src/main/scala/no/ndla/database/DBMigrator.scala

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ trait DBMigrator {
1717

1818
case class DBMigrator(migrations: JavaMigration*) {
1919
def migrate(): Unit = {
20+
DataSource.connectToDatabase()
21+
2022
val config = Flyway
2123
.configure()
2224
.javaMigrations(migrations*)

draft-api/src/main/scala/no/ndla/draftapi/ComponentRegistry.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,8 @@ class ComponentRegistry(properties: DraftApiProperties)
9898
new V57__MigrateSavedSearch,
9999
new V66__SetHideBylineForImagesNotCopyrighted
100100
)
101-
override val dataSource: HikariDataSource = DataSource.getHikariDataSource
101+
override lazy val dataSource: HikariDataSource = DataSource.getHikariDataSource
102102
override val DBUtil: DBUtility = new DBUtility
103-
DataSource.connectToDatabase()
104103

105104
lazy val draftRepository = new DraftRepository
106105
lazy val userDataRepository = new UserDataRepository

frontpage-api/src/main/scala/no/ndla/frontpageapi/ComponentRegistry.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ class ComponentRegistry(properties: FrontpageApiProperties)
4141
with SwaggerDocControllerConfig {
4242
override val props: FrontpageApiProperties = properties
4343
override val migrator: DBMigrator = DBMigrator()
44-
override val dataSource: HikariDataSource = DataSource.getHikariDataSource
45-
DataSource.connectToDatabase()
44+
override lazy val dataSource: HikariDataSource = DataSource.getHikariDataSource
4645

4746
override val clock = new SystemClock
4847

image-api/src/main/scala/no/ndla/imageapi/ComponentRegistry.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ class ComponentRegistry(properties: ImageApiProperties)
7272
new V6__AddAgreementToImages,
7373
new V7__TranslateUntranslatedAuthors
7474
)
75-
override val dataSource: HikariDataSource = DataSource.getHikariDataSource
76-
DataSource.connectToDatabase()
75+
override lazy val dataSource: HikariDataSource = DataSource.getHikariDataSource
7776

7877
lazy val s3Client = new NdlaS3Client(props.StorageName, props.StorageRegion)
7978

learningpath-api/src/main/scala/no/ndla/learningpathapi/ComponentRegistry.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ class ComponentRegistry(properties: LearningpathApiProperties)
8686
new V31__ArenaDefaultEnabledOrgs,
8787
new V33__AiDefaultEnabledOrgs
8888
)
89-
override val dataSource: HikariDataSource = DataSource.getHikariDataSource
90-
DataSource.connectToDatabase()
89+
override lazy val dataSource: HikariDataSource = DataSource.getHikariDataSource
9190

9291
lazy val learningPathRepository = new LearningPathRepository
9392
lazy val readService = new ReadService

myndla-api/src/main/scala/no/ndla/myndlaapi/ComponentRegistry.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ class ComponentRegistry(properties: MyNdlaApiProperties)
9494
lazy val DBUtil = new DBUtility
9595

9696
override val migrator: DBMigrator = DBMigrator(v16__MigrateResourcePaths)
97-
override val dataSource: HikariDataSource = DataSource.getHikariDataSource
98-
DataSource.connectToDatabase()
97+
override lazy val dataSource: HikariDataSource = DataSource.getHikariDataSource
9998

10099
val swagger = new SwaggerController(
101100
List(

0 commit comments

Comments
 (0)