简体   繁体   中英

Inject database connection pool

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 DBComponents and BoneCPComponents traits in the RepositoryModule . With them, I could get a Database object and inject it to the repositories. This is the code I'm using for the module:

trait RepositoryModule extends BuiltInComponents with DBComponents with BoneCPComponents {
  lazy val database: Database = dbApi.database("default")
  lazy val userRepository = wire[UserRepository]
}

The database "default" will use the db.default configurations in the application.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 the withConnection method.

An example of the user repository using the injected database:

class UserRepository(db: Database) {
  def deleteAdults: Unit = {
    val connection: Connection = db.getConnection()
    val removed: Int = SQL("DELETE * FROM User WHERE age > 18").executeUpdate()(connection)
    connection.close()
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM