1
votes

I'm new in Play Framework and used to manage transactions in java/spring style with controller, transactional service and dao layers. It's pretty usual case for me to have multiply operations with dao in service method and make him @Transactional to rollback all changes if something goes wrong. Service isolated from dao and know nothing about database.

But I didn't find something like this in Anorm framework and Play. All logic placed in controllers and you can make transaction only this ugly way - Database transactions in Play framework scala applications (anorm)

We have several problems here:

  1. Service turns into dao
  2. If we need to call same dao method from another service we have change it same way

Is there nice way to manage transactions in Play? Other frameworks like Slick? How to use Play in production with such restricments

1
See DB.withTransaction - cchantep
it was in question like example of ugly solution. Or I don't know how to cook it right... - Denis Loshkarev
That's right, if you find it the ugly way you need to read the doc :) - cchantep

1 Answers

1
votes

Anorm's DB.withTransaction creates and commits a transaction when it exits, so there is no out-of-the-box support for your use case. Though it is quite straightforward to create your own transaction engine based on what Anorm offers that spans multiple services: it creates a transaction if none is present in ThreadLocal and stores it there or uses the one obtained from it in subsequent 'transactional' usages. Then you could have one, big transaction that rollbacks on an error deep down the dao layer. We have a solution like this in production and works just fine.

However, there is a conceptual problem, that you should be aware of. As soon as you need to call a service that returns a Future you no longer have the transaction (you are possibly on another thread) or you should block (which is not a good thing in production).