问题
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.
回答1:
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();
}
来源:https://stackoverflow.com/questions/9703688/how-to-integrate-the-scala-squeryl-orb-with-play-2-0-framework