Panzerschlacht
Panzerschlacht

Reputation: 67

Slick query very slow

I'm trying to learn Slick and have got a Postgres database up and running. I made a tiny program to test it like this:

import scala.slick.driver.PostgresDriver.simple._
import Database.threadLocalSession

object Names extends Table[(String)]("names")     
{
    def name = column[String]("name", O.PrimaryKey)
    def * = name
}

object DbTest
{
    val db = Database.forURL("jdbc:postgresql://localhost:5432/names", 
                              driver = "org.postgresql.Driver")

    def main(args : Array[String]) =
    {
        print("Doing something... ")

        db withTransaction 
        {
            Query(Names) foreach 
            { 
                case (name) =>
                    println(name)
            }
        }
        println("... done!")
    }
}

The problem is that it takes about 5 seconds for anything to happen after print("Doing something... "). If I duplicate the db withTransaction block, both blocks are executed in quick succession after those first 5 seconds. Any ideas?

Upvotes: 5

Views: 1457

Answers (1)

Taylor Leese
Taylor Leese

Reputation: 52400

Not sure I know the specific problem, but there are a few things in your code example that would be a concern for performance.

1) Make sure you are using Query Templates. There is quite a bit of overhead building the query with Slick that you don't want to repeat on each query.

2) You also shouldn't use threadLocalSession in real code (see thread here). It should look something like below.

3) Use a connection pool like C3P0.

Example:

val pool = // some connection pool like C3P0 or other

Database.forDataSource(pool).withSession { implicit session: Session =>
  ...
}

Upvotes: 2

Related Questions