如何将Scala Squeryl ORB与play 2.0框架集成? [英] How to integrate the Scala Squeryl ORB with play 2.0 framework?
问题描述
我正在尝试将Squeryl ORB与play 2.0框架一起使用,但是在初始化期间调用DB.getConnection()
时,我得到了:
I am trying to use Squeryl ORB with play 2.0 framework, but when calling DB.getConnection()
during initialization I get:
BadPath:路径参数:无效路径'-找不到defaultdb的数据源':路径表达式中不允许使用令牌:'-'(如果您确实希望在此处使用此令牌,可以将其双引号)
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)
数据库配置如下所示(conf/application.conf):
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
和初始化:
object Global extends GlobalSettings {
override def onStart(app: Application) {
SessionFactory.externalTransactionManagementAdapter = Some(() =>
Some(new Session(
DB.getConnection("defaultdb", true),
new PostgreSqlAdapter)))
...
这是正确的方法吗?将db.default.jndiName
配置值用作DB.getConnection()
的参数值是否正确?
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()
?
还是应该这样做?:
SessionFactory.concreteFactory = Some(() =>
Session.create(
java.sql.DriverManager.getConnection("jdbc:postgresql://..."),
new PostgreSqlAdapter))
这行得通,但是后来我无法使用模板中的squeryl查询对象进行迭代,我希望可以使用externalTransactionManagementAdapter
来实现.
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
.
我更正了以下内容:DB.getConnection("default", true)
并删除了db.default.jndiName
配置.
这样我就可以获取并使用连接,但是第二次调用getConnection()
时,它会抛出SQLException: Timed out waiting for a free available connection.
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.
我没有设法使用externalTransactionManagementAdapter
,但是concreteFactory
效果很好-如下所述.
I haven't managed to use externalTransactionManagementAdapter
, but concreteFactory
works well - as described below.
推荐答案
下一个对我有用:
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();
}
这篇关于如何将Scala Squeryl ORB与play 2.0框架集成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!