3

As I understand from docs of Scalikejdbc if I want to have a transaction I need to pass a session. Like this:

  class FooBarService {

    val fooDao = new FooDao
    val barDao = new BarDao

    def fooBar(): Unit = {
      DB localTx { session =>
        fooDao.foo(new Foo())
        barDao.bar(new Bar())
      }
    }
  }

  class FooDao {
    def foo(foo: Foo)(implicit session: DBSession): Unit = {
      sql"""bla bla bla"""
    }
  }

  class BarDao {
    def bar(bar: Bar)(implicit session: DBSession): Unit = {
      sql"""bla bla bla"""
    }
  }

And that thing makes me explicitly dependent on Scalikejdbc. I don't want to have a dependency on Scalike in my domain traits. What I want is something like this:

class FooBarService {

    val fooDao = new FooDao
    val barDao = new BarDao

    def fooBar(): Unit = {
      // My wrapper which abstract and is not dependent on Scalike or other library
      MyWrapper.withinTransaction {
        fooDao.foo(new Foo())
        barDao.bar(new Bar())
    }
      // or at least not to pass session to daos
      DB withinTransaction {
        fooDao.foo(new Foo())
        barDao.bar(new Bar())
      }
    }
  }

  class FooDao {
    def foo(foo: Foo): Unit = {
      sql"""bla bla bla"""
    }
  }

  class BarDao {
    def bar(bar: Bar): Unit = {
      sql"""bla bla bla"""
    }
  }

Is there a way to have transactions and do not have dependencies on sessions and scalike in class signature?

Artem Malinko
  • 1,761
  • 1
  • 22
  • 39
  • Did you ever find a solution? – jen Mar 07 '18 at 10:22
  • @jen no. We've switched to Slick. Scalikejdbc and it's transactions bothered me because I didn't want to add third-party dependency to my methods signatures. Slick has somewhat similar issues(we need to expose DBIOAction if we want combine them in transaction), but now I also think that this is not a problem. – Artem Malinko Mar 07 '18 at 13:06