Database transactions in Play framework scala applications (anorm)

后端 未结 2 2046
耶瑟儿~
耶瑟儿~ 2021-01-22 17:04

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.

Brief:

2条回答
  •  鱼传尺愫
    2021-01-22 17:42

    What about

    class Dao {
      def foo(id: Long)(implicit connection: Connection) = {
        SQL("select * from foo where id={id}").on('id->id).as(...)
      }
    }
    
    class Service{
      def withConnection = {
        DB.withConnection {implicit connection =>
          Dao.foo(1)
          Dao.foo(2)
        }
      }
    
      def withTransaction = {
        DB.withTransaction {implicit connection =>
          Dao.foo(1)
          Dao.foo(2)
      }
    }
    

提交回复
热议问题