spidla
spidla

Reputation: 231

Play 2.4 - using Ebean in eager singleton module

I have got a question.

I am using Play Framework with Ebean. I am trying to port my application to Play 2.4 and I am trying to use dependency injection technique introduced in Play.

In previous version I was using GlobalSettings.onStart() to initialize some things after the application starts (cron-like service). In 2.4 I am trying to implement "onStart" process as eagerly loaded singleton using dependency injection framework.

Generally speaking, it is working. However, sometimes, I am getting NullPointerException while starting application. From stack trace, I can assume, that the exception is happening while accessing database (EbeanServer is null in that time). So I assumed, that Ebean has not properly started because eager singletons are initiliased before application starts. OK then, I injected Play`s Application and Database objects into the eager singleton constructor in order to start everything up in time.

The results are ... unstable. Sometime application starts fine, sometime it falls in the same NullPointerException.

Looks like the database is sometimes not started in time or what? Or maybe I need to inject some other Ebean object in order to start database?

Upvotes: 4

Views: 634

Answers (1)

spidla
spidla

Reputation: 231

OK I figured that out.

I had to inject play.db.ebean.EbeanConfig and play.api.db.evolutions.DynamicEvolutions and now Ebean is starting earlier as a dependency of my eager singleton.

These are the exact two classes, that are injected while loading play-ebean plugin as you can see here https://github.com/playframework/play-ebean/blob/master/play-ebean/src/main/java/play/db/ebean/EbeanModule.java

Upvotes: 3

Related Questions