ScalikeJDBC:连接池尚未初始化。(名称:'default') [英] ScalikeJDBC: Connection pool is not yet initialized.(name:'default)

查看:562
本文介绍了ScalikeJDBC:连接池尚未初始化。(名称:'default')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm playing with ScalikeJdbc library. I want to retrieve the data from PostgreSQL database. The error I get is quite strange for me. Even if I configure manually the CP:

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

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

I still see the error. Here is my 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)

我错过了什么?

推荐答案

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

To load application.conf, scalikejdbc-config's DBs.setupAll() should be called in advance.

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

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

这篇关于ScalikeJDBC:连接池尚未初始化。(名称:'default')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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