【问题标题】:How to use DBIO.sequence and avoid StackOverflowError如何使用 DBIO.sequence 并避免 StackOverflowError
【发布时间】:2015-03-25 11:00:05
【问题描述】:

我是一个 Slick 初学者,正在尝试使用 Slick 3.0 RC1。在我的第一个项目中,我想将数据从文本文件导入到各种表中。整个导入应按顺序进行,因为数据在一个事务中的文件中。

我尝试创建操作的迭代器并将它们包装在 DBO.sequence 中。

问题是当行数很大时,导入失败并出现 StackOverflowError。显然我误解了如何使用 Slick 来做我想做的事情。有没有更好的方法如何将大量数据链接到一个交易中?

这是我的代码的简化版本,我不是从文件中读取数据,而是简单地从 Range 中“导入”数字。 XS 表的偶数,YS 表的奇数。

val db = Database.forConfig("h2mem1")

try {

  class Xs(tag: Tag) extends Table[(Long, String)](tag, "XS") {
    def id = column[Long]("ID", O.PrimaryKey)
    def name = column[String]("NAME")
    override def * : ProvenShape[(Long, String)] = (id, name)
  }

  class Ys(tag: Tag) extends Table[(Long, String)](tag, "YS") {
    def id = column[Long]("ID", O.PrimaryKey)
    def name = column[String]("NAME")
    override def * : ProvenShape[(Long, String)] = (id, name)
  }

  val xs = TableQuery[Xs]
  val ys = TableQuery[Ys]


  val setupAction = DBIO.seq((xs.schema ++ ys.schema).create)

  val importAction = DBIO.sequence((1L to 100000L).iterator.map { x =>
    if (x % 2 == 0) {
      xs +=(x, x.toString)
    } else {
      ys +=(x, x.toString)
    }
  })

  val f = db.run((setupAction andThen importAction))

  Await.result(f, Duration.Inf)

} finally {
  db.close
}

【问题讨论】:

    标签: slick slick-3.0


    【解决方案1】:

    这个问题是由于在 RC1 版本的 Slick 中实现效率不高造成的。如问题线程here 所示,该实现已得到改进。

    在 Slick 3 RC 3 中问题解决了 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-23
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 2017-07-09
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多