Managing Scala Slick Database Drivers & Type-Safeing Session Concurrency

Eishay Smith
Keep It Up
Published in
2 min readJul 17, 2013

In this post we’ll describe how we use MySQL in production and H2 DB in test mode. We’re using Slick for database access with Play Framework and Guice.

The base implementation is inspired by a post from the ScalaQuery newsgroup. The top level abstraction we use is DataBaseComponent:

In our codebase there are two implementations of DataBaseComponent:

  • H2 which we use in test and dev
  • MySQL which we use in production and sometimes in dev if we want to run against sample data from production

H2 implementation:

MySQL implementation:

Initializing the relevant Slick driver is done in the Guice module. It’s a bit specific to the way we’re using Guice, but this code should give you an idea of how it’s done:

And in the module installation phase, we can do:

In tests, we hard-code drivers and data sources; in our dev/prod environments, we let the Play application config handle that.

The class that actually manages the sessions for the common code is Database.

As you can see in the above code, we force callers to explicitly choose either a read-only session (a Slick session wrapped in a ROSession) or a read-write session (a Slick session wrapped in a RWSession).

So the usage of our database API looks like this:

Originally published at eng.kifi.com on July 17, 2013.

--

--

Eishay Smith
Keep It Up

Founder & CEO at Kifi: Connecting People with Knowledge