title: Database transactions in Play framework scala applications (anorm)
body: <p>I am developing an application using Play framework and scala. I am using anorm for data-access layer. And I've got a problem I could not solve.</p>
  
  <p><strong>Brief:</strong> I want to be able to have methods in data-access objects (dao) to work inside transactions as well as being called alone.</p>
  
  <p><strong>Details:</strong></p>
  
  <p>I have data-access layer consist of class with methods that only executes particular SQL over database. Traditionally they looks like:</p>
  
  <pre><code>def list() = DB.withConnection { implicit cn =&gt;
    ...
  }
  </code></pre>
  
  <p>Now I want to have some methods to be executed in a transaction scope. Like traditional select-update service methods but still be able to run them alone. So, what I have in my mind is like this:</p>
  
  <pre><code>class Service {
    def fooTransacted() = {
      inTransaction {
        val old = dao.select(id = 2)
        val newObj = old.copy(isActive = true)
        dao.update(newObj)
      }
    }
  
    def fooSinle() = {
      dao.select(id = 2)
    }
  }
  </code></pre>
  
  <p>I tried around several ways, but could not come up with any solution.</p>