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
.
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.
I haven't managed to use externalTransactionManagementAdapter
, but concreteFactory
works well - as described below.
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.
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
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:
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
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With