当前位置:  开发笔记 > 编程语言 > 正文

ScalikeJDBC:连接池尚未初始化.(名称:'默认)

如何解决《ScalikeJDBC:连接池尚未初始化.(名称:'默认)》经验,为你挑选了1个好方法。

我正在玩ScalikeJdbc图书馆.我想从PostgreSQL数据库中检索数据.我得到的错误对我来说很奇怪.即使我手动配置CP:

val poolSettings = new ConnectionPoolSettings(initialSize = 100, maxSize = 100)
ConnectionPool.singleton("jdbc:postgresql://localhost:5432/test", "user", "pass", poolSettings)

我仍然看到错误.这是我的DAO:

class CustomerDAO {

  case class Customer(id: Long, firstname: String, lastname: String)
  object Customer extends SQLSyntaxSupport[Customer]

  val c = Customer.syntax("c")

  def findById(id: Long)(implicit session: DBSession = Customer.autoSession) =
    withSQL {
      select.from(Customer as c)
    }.map(
      rs => Customer(
        rs.int("id"),
        rs.string("firstname"),
        rs.string("lastname")
      )
    ).single.apply()

}

该应用程序:

object JdbcTest extends App {
  val dao = new CustomerDAO
  val res: Option[dao.Customer] = dao.findById(2)
}

我的application.conf档案

# PostgreSQL
db.default.driver = "org.postgresql.Driver"
db.default.url = "jdbc:postgresql://localhost:5432/test"
db.default.user = "user"
db.default.password = "pass"

# Connection Pool settings
db.default.poolInitialSize = 5
db.default.poolMaxSize = 7
db.default.poolConnectionTimeoutMillis = 1000

错误:

Exception in thread "main" java.lang.IllegalStateException: Connection pool is not yet initialized.(name:'default)
    at scalikejdbc.ConnectionPool$$anonfun$get$1.apply(ConnectionPool.scala:57)
    at scalikejdbc.ConnectionPool$$anonfun$get$1.apply(ConnectionPool.scala:55)
    at scala.Option.getOrElse(Option.scala:120)
    at scalikejdbc.ConnectionPool$.get(ConnectionPool.scala:55)
    at scalikejdbc.ConnectionPool$.apply(ConnectionPool.scala:46)
    at scalikejdbc.NamedDB.connectionPool(NamedDB.scala:20)
    at scalikejdbc.NamedDB.db$lzycompute(NamedDB.scala:32)

我错过了什么?



1> Kazuhiro Ser..:

要加载application.conf,DBs.setupAll()应提前调用scalikejdbc-config .

http://scalikejdbc.org/documentation/configuration.html#scalikejdbc-config

https://github.com/scalikejdbc/hello-scalikejdbc/blob/9d21ec7ddacc76977a7d41aa33c800d89fedc7b6/test/settings/DBSettings.scala#L3-L22

推荐阅读
pan2502851807
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有