Play with Reactive Slick: A Simple CRUD application in Play! framework using Slick 3.0

Table of contents
Reading Time: 2 minutes

Recently Typesafe released Slick 3.0.0-RC1, the first release candidate for “Reactive Slick”. Reactive Slick has many new features like Support for Reactive Streams API, a new API for composing and executing database actions and many more.

Before moving forward, we recommended you to go through the Slick 3.0.0-RC1 Upgrade guide. So, that you can get a hint of major changes in Slick 3.0.

In this post, we will see how to build a Reactive Play application with Reactive Slick. In order to build a simple CRUD application in Play framework using Reactive Slick (Slick-3.0.0-RC1) we need to follow these steps:

1) Run activator new play-reactive-slick play-scala to create a new Play application.

2) Add following dependencies in build.sbt file

Here we are using PostgreSQL as database. So, if you have not installed it yet, you can download it from here.

3) Next, we need to add bidirectional mapping of our case class with the table, like this:

Important thing to note in above mapping is that we have omitted O.Nullable/O.NotNull. The reason behind it is that both O.Nullable/O.NotNull are deprecated in Slick-3.0.0-RC1. Instead, we have provided the mapping for optional fields in projection itself.

4) Then, we can write a Database Access Object (DAO) to perform CRUD operations:

As we can see in above code that now Slick returns result in Future. Also, now the database instance db have an associated connection pool and thread pool, so, it is important to call db.close when we are done using it.

5) At last, we need to provide Asynchronous Action response in controller. Example:

Here we are using Action.async to handle Future response from database.

In this way we can build a simple CRUD application in Play framework using Reactive Slick. For more information on Reactive Slick click here.

To download a demo Application click here.

Written by 

Himanshu Gupta is a software architect having more than 9 years of experience. He is always keen to learn new technologies. He not only likes programming languages but Data Analytics too. He has sound knowledge of "Machine Learning" and "Pattern Recognition". He believes that best result comes when everyone works as a team. He likes listening to Coding ,music, watch movies, and read science fiction books in his free time.

18 thoughts on “Play with Reactive Slick: A Simple CRUD application in Play! framework using Slick 3.03 min read

  1. Thank you for the article, looks really interesting. I took a look at the source to recreate the project and was wondering where your evolutions are coming from. They appear to be auto generated but that is not happening for me, even though I specified the slick.default property in the config file. Is there a way to trigger the generation manually or how is it working for you?

  2. /** Free all resources allocated by Slick for this Database object. In particular, the
    * [[slick.util.AsyncExecutor]] with the thread pool for asynchronous execution is shut
    * down. If this object represents a connection pool managed directly by Slick, it is also
    * closed. */
    def close: Unit = try executor.close() finally source.close()

  3. I vote down this post because some newbie I know wrote “finally { db.close()} ” in model class after reading this.

  4. I tried to change build.sbt, application.conf and then created correspondent table (Employee) on MySQL database. But I had the error message: “[MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘”date_of_birth” as x6, x9.”address” as x5, x9.”designation” as x8, x9.”id” as x3’ at line 1]”

    I tried to debug but couldn’t find the cause.
    Could you have a help?
    Thanks so much.

    1. Sorry! I my previous post I forgot to write that I want to modify current source codes to run on MySql database instead of PostgresSQL

  5. In your DAO object, when you call db.run(), db.close() you are calling the method db. This might give you an other database object which flaws the run/close flow, doesn’t it?

  6. I wonder why you don’t get an exception like: “[PSQLException: Bad value for type long : 2015-04-06 22:36:17]”

  7. Do you have any advice concerning mapped vs. non-mapped tables? Your are using mapped table, but
    tuple seems to be the “default” way since it appears in almost all the examples in the documentation and mapped tables only in a dedicated section. Where is the advantage of using tuples, the access is index based and not really type safe. Maybe I’m missing something.

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading