8

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)

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()

}

The App:

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

My application.conf file

# 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

The error:

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)

What did I miss?

Finkelson
  • 2,921
  • 4
  • 31
  • 49

2 Answers2

9

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

Kazuhiro Sera
  • 1,822
  • 12
  • 15
  • 1
    @Finkelson Can you post details how you resolved this issue? – sergeda Aug 09 '16 at 21:45
  • Recently, I ran into this problem. I have always read the db-settings from (`typesafe`) `config` files manually and then created a `ConnectionPool` using those settings, so the `DBs.setupAll()` solution wasn't relevant for me. I tried waiting for the initialization using `while(!ConnectionPool.isInitialized(name)) {}` but that also didn't work. My fix was rather strange: changing `ConnectionPool.add(name="", ...)` to `ConnectionPool.singleton(...)` got rid of the error. I wonder how moving from *custom-named* `ConnectionPool` to *default -named* `ConnectionPool` could alter the behaviour. – y2k-shubham Apr 28 '18 at 07:04
5

In my case I omit play.modules.enabled += "scalikejdbc.PlayModule" in conf/application.conf using ScalikeJDBC Play support...

neske
  • 349
  • 4
  • 7