Search code examples
playframeworksqueryl

How to integrate the Scala Squeryl ORB with play 2.0 framework?


I am trying to use Squeryl ORB with play 2.0 framework, but when calling DB.getConnection() during initialization I get:

BadPath: path parameter: Invalid path ' - could not find datasource for defaultdb': Token not allowed in path expression: '-' (you can double-quote this token if you really want it here)

The database configuration looks like this (conf/application.conf):

db.default.url="jdbc:postgresql://localhost/mydb?user=postgres&password=postgres"
db.default.driver=org.postgresql.Driver
db.default.jndiName=defaultdb

And the initializing:

object Global extends GlobalSettings {
  override def onStart(app: Application) {

    SessionFactory.externalTransactionManagementAdapter = Some(() => 
        Some(new Session(
          DB.getConnection("defaultdb", true),
          new PostgreSqlAdapter)))
    ...

Is this the right way to do it? Is it correct to use the db.default.jndiName config value as parameter value to DB.getConnection()?

Or should it be done like this?:

  SessionFactory.concreteFactory = Some(() =>
    Session.create(
      java.sql.DriverManager.getConnection("jdbc:postgresql://..."),
      new PostgreSqlAdapter))

This works, but then I am not able to use the squeryl query objects in the template for iteration, which I hoped would be possible with externalTransactionManagementAdapter.

Update:

I corrected to the following: DB.getConnection("default", true) and removed the db.default.jndiName config. With this I am able to get and use a connection, but the second time getConnection() is called, it throws SQLException: Timed out waiting for a free available connection.

Update 2:

I haven't managed to use externalTransactionManagementAdapter, but concreteFactory works well - as described below.


Solution

  • Next works for me:

    import play.db.DB 
    import play.api.Application 
    import play.api.GlobalSettings 
    import org.squeryl._ 
    import org.squeryl.adapters._ 
    

    ....

    object Global extends GlobalSettings
    {
    
    override def onStart(app:Application):Unit =
    {
     SessionFactory.concreteFactory = Some(
          () => Session.create(DB.getDataSource().getConnection(),
                               dbAdapter)
     );
    }
    
    override def onStop(app:Application):Unit =
    {
    }
    
    val dbAdapter = new PostgreSqlAdapter();
    
    }