3

正如我从 Scalikejdbc 的文档中了解到的那样,如果我想要进行交易,我需要通过会话。像这样:

  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"""
    }
  }

而那件事让我明确地依赖于 Scalikejdbc。我不想在我的领域特征中依赖 Scalike。我想要的是这样的:

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"""
    }
  }

有没有办法进行交易并且不依赖会话和类签名中的scalike?

4

1 回答 1

0

如果您的应用程序不是非阻塞架构,则可以将线程本地用于 DB 会话。

于 2016-04-02T01:50:14.913 回答