I have started using MacWire for the dependency injection of my Play app, and I am having problems trying to inject the database connection.
Before using DI, my code looked like this:
DB.withConnection { implicit connection =>
...
}
This is not working anymore after using DI. I get the following exception: java.lang.InstantiationException: play.api.db.DBApi.
My application loader:
class Loader extends ApplicationLoader {
def load(context: Context) = {
val components = new BuiltInComponentsFromContext(context) with Components
components.application
}
}
The main components of the app:
trait Components extends BuiltInComponents with I18nComponents
with RepositoryModule {
lazy val assets: Assets = wire[Assets]
lazy val router: Router = wire[Routes] withPrefix "/"
}
And the repository module:
trait RepositoryModule {
lazy val userRepository = wire[UserRepository]
}
How can I get and use a database connection pool and inject it so it can be used within the repository?
I solved it using the
DBComponentsandBoneCPComponentstraits in theRepositoryModule. With them, I could get aDatabaseobject and inject it to the repositories. This is the code I'm using for the module:The database "default" will use the
db.defaultconfigurations in theapplication.conf. The main problem with this solution is that you need to get the connections from the pool and return them when you're done. I don't know if it can be improved to mimic thewithConnectionmethod.An example of the user repository using the injected database: