Scala Slick 2.0 for multi-database

Here i am going to explain how to use Slick 2.0 (Lifted Embedding API) in an application which have  multiple-database for multiple environment . In any application generally we have different environments like testing, development, production etc.

I want to demonstrate this concept using simple use case. lets say we want to use  H2 for testing ,MySql for development and PostgreSql for production environment. In database  there is an employee table which have ID, NAME, EMAIL, DESIGNATION, DOJ (date of joining) as columns.  This table contains some records. We need to perform basic database operation on this table using slick. Database must be selected at runtime on the bases of run mode.

Here is the Scala & Slick way to implementing this functionality.  The logic has been extracted out in three main classes.

a) domain.scala    b) SlickDBDriver.scala c)DBConnection.scala

a) domain.scala

As It is required to access different database in different environment so  database connection properties like url, username, password is required.We have externalized these properties in  three configuration file.

test.conf

dev.conf

prod.conf

Slick will take the property from appropriate config file and create the database connection with respect to the environment in which application is running.

b)SlickDBDriver.scala : By this object get slick database drive with respect to selected run mode.

DBConnection.scala : This class creates database object using which we can access database.

Now we can access  multiple database by SLICK :

SlickDemoApp.scala :

Now set environment and run the application:

for test:
sky@Sky:~/knols_sessions/slickdemo/slick2demo$ export runMode=test
sky@Sky:~/knols_sessions/slickdemo/slick2demo$ sbt run

for dev:
sky@Sky:~/knols_sessions/slickdemo/slick2demo$ export runMode=dev
sky@Sky:~/knols_sessions/slickdemo/slick2demo$ sbt run

for prod:
sky@Sky:~/knols_sessions/slickdemo/slick2demo$ export runMode=prod
sky@Sky:~/knols_sessions/slickdemo/slick2demo$ sbt run

Download complete code example from  here

Enjoy with Slick !!!!

Written by 

Rachel Jones is a Solutions Lead at Knoldus Inc. having more than 22 years of experience. Rachel likes to delve deeper into the field of AI(Artificial Intelligence) and deep learning. She loves challenges and motivating people, also loves to read novels by Dan Brown. Rachel has problem solving, management and leadership skills moreover, she is familiar with programming languages such as Java, Scala, C++ & Html.

12 thoughts on “Scala Slick 2.0 for multi-database

  1. This was a really helpful post, thanks.
    I’ve had a bit of trouble with the very last part though. I don’t understand where the ‘export’ command comes from in lines like:
    ~/knols_sessions/slickdemo/slick2demo$ export runMode=test

    When I try that command, I get:
    ‘export’ is not recognized as an internal or external command,
    operable program or batch file.

    So I can’t figure out how to actually change between test, dev and prod modes. Any help would be much appreciated.
    Cheers

      1. This example works on linux OS. Because in linux you can set environment variable by export command on terminal.
        Could you try => set runMode=test . Right now did’t have windows OS so I am not sure this wil work. Let me know if it works or not ?

      2. Ah right, I didn’t realise export was a Linux command. set runMode=test worked perfectly. Thanks very much

    1. Suppose my application have multiple environment(dev,prod,test) and each environment have different database.
      Let’s say prod -> postgres, test -> h2 . In this scenario ,We need to define a trait which have driver level abstraction.Let’s define Profile trait :

      trait Profile {
      val profile: JdbcProfile
      }
      Now another question , Why JdbcProfile trait ?
      Because , Each database driver have implemented JdbcProfile trait(for more info http://slick.typesafe.com/doc/2.1.0/api/#scala.slick.driver.JdbcProfile)

  2. Hi,
    Just an off topic question .. Is there a way to handle more than 22 columns in a table using slick ?

  3. @Jose Carlos: Thanks for feedback.
    Database.forURL exists at version 3.0.3.
    please see doc => http://slick.typesafe.com/doc/3.0.3/api/index.html#slick.jdbc.JdbcBackend$DatabaseFactoryDef
    and also see source code line no 100 at => https://github.com/slick/slick/blob/615fc0da4d130c5f25d67b629bdb60a0d20fb27e/slick/src/main/scala/slick/jdbc/JdbcBackend.scala

    This blog post have been written for slick 2.0. So there are lot of changes from slick2.0 to Slick 3.0.3.
    If you want use Slick 3.0.3 please see my activator template =>https://github.com/knoldus/slick-starting-on-the-right-foot

    Let me know any if issue.

Leave a Reply

%d bloggers like this: