Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slick Database Sessions, Efficiency, and Threading in Akka

Tags:

scala

akka

slick

I am currently working with a very large database (>50GB) and trying to understand the most efficient, usable approach that plays well with Akka's inherent threading.

Regarding the "wrapping everything inside withSession{ }" approach, while this would be an easier fix, I am concerned that this would restrict Akka's threading between actors. I am not that knowledgeable on how Akka's threading works, and how wrapping an entire actor system inside of withSession would effect it.

Another approach is to call withSession whenever the database is accessed, which is too inefficient. The "withSession {" code segment takes ~6ms to execute, and we are making millions of queries.

Essentially: what is the best way to rapidly access a database with Slick and Akka without breaking threading?

I have heard of approaches using implicit sessions and transactions, but I am struggling to find documentation on either of these.

like image 257
TreeWhale Avatar asked Jul 06 '13 03:07

TreeWhale


1 Answers

Better late than never:

The recommended way is using a jdbc connection pool (e.g. c3p0). You need to make sure a session is acquired and returned from and in between kept on the same thread. withSession lazily acquires a connection from the pool and returns it at the end of the scope. Quickly acquire connections when you need them and return them to the pool immediately after.

like image 135
cvogt Avatar answered Nov 20 '22 11:11

cvogt