如何将 Scala Squeryl ORB 与 play 2.0 框架集成? [英] How to integrate the Scala Squeryl ORB with play 2.0 framework?

查看:22
本文介绍了如何将 Scala Squeryl ORB 与 play 2.0 框架集成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 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: path parameter: Invalid path '- could not find datasource for defaultdb': Token not allowed in path expression: '-' (如果你真的想要这个标记,你可以在这里用双引号引起来)

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.

推荐答案

Next 对我有用:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆