Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 464
Roar Skullestad Avatar asked Mar 14 '12 14:03

Roar Skullestad


People also ask

How to build a Play Framework application using sbt?

The Play Framework documentation promises us “a powerful console and build tools”. We can start by generating the scaffolding of a new Play Framework application using sbt: After loading a lot of dependencies, the tool displays a prompt and asks us to name the new project and provide the organization name.

How do I connect to a database in Scala?

To connect to our database, we’ll use Slick, which is a database driver that gives us the benefit of static checking, compile-time safety, and compositionality of Scala alongside plain SQL. This basically adds the Slick library as well as other dependencies. 2.1. PostgreSQL

How to implement a new action in a Scala controller?

To implement a new action, we open the Scala file ( HomeController.scala) and add a new method that accepts two parameters, calculates their sum, and passes the result to the view template: Now, let’s open the index.scala.html file, add the sum parameter on top of the file, and use it in the content:

What is the directory structure of a Scala project?

Project Structure Now, it’s time to load the project code into the IDE and look at the directory structure. In our project directory, we see four directories created by the sbt template: app/controllers, app/views, conf, and public. The controllers directory is where we will store our Scala code


1 Answers

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();

}
like image 200
rssh Avatar answered Feb 22 '23 22:02

rssh