gpt4 book ai didi

postgresql - 批量插入时 Slick 3.0 中的数据库异常

转载 作者:行者123 更新时间:2023-11-29 11:36:56 24 4
gpt4 key购买 nike

虽然在 slick 3 中通过批量插入每五秒插入数千条记录,但我得到了

org.postgresql.util.PSQLException: FATAL: sorry, too many clients already

我的数据访问层是这样的:

val db: CustomPostgresDriver.backend.DatabaseDef = Database.forURL(url, user=user, password=password, driver= jdbcDriver)



override def insertBatch(rowList: List[T#TableElementType]): Future[Long] = {
val res = db.run(insertBatchQuery(rowList)).map(_.head.toLong).recover{ case ex:Throwable=> RelationalRepositoryUtility.handleBatchOperationErrors(ex)}
//db.close()
res
}

override def insertBatchQuery(rowList: List[T#TableElementType]): FixedSqlAction[Option[Int], NoStream, Write] = {
query ++= (rowList)
}

在插入批处理中关闭连接没有效果...它仍然给出相同的错误。

我像这样从我的代码中调用插入批处理:

val temp1 = list1.flatMap { li =>
Future.sequence(li.map { trip =>
val data = for {
tripData <- TripDataRepository.insertQuery( trip.tripData)
subTripData <- SubTripDataRepository.insertBatchQuery(getUpdatedSubTripDataList(trip.subTripData, tripData.id))
} yield ((tripData, subTripData))
val res=db.run(data.transactionally)
res
//db.close()
})
}

如果我在这里工作后关闭连接,如您在注释代码中看到的那样,我会收到错误消息:

java.util.concurrent.RejectedExecutionException: Task slick.backend.DatabaseComponent$DatabaseDef$$anon$2@6c3ae2b6 rejected from java.util.concurrent.ThreadPoolExecutor@79d2d4eb[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1]

像这样调用没有 Future.sequence 的方法后:

 val temp1 =list.map { trip =>
val data = for {
tripData <- TripDataRepository.insertQuery( trip.tripData)
subTripData <- SubTripDataRepository.insertBatchQuery(getUpdatedSubTripDataList(trip.subTripData, tripData.id))
} yield ((tripData, subTripData))
val res=db.run(data.transactionally)
res
}

我仍然有太多客户端错误...

最佳答案

这个问题的根源在于您同时启动了一个无界的 Future 列表,每个列表都连接到数据库 - list 中的每个条目一个。

这可以通过串行运行插入来解决,强制每个插入批处理依赖于前一个:

// Empty Future for the results. Replace Unit with the correct type - whatever
// "res" is below.
val emptyFuture = Future.successful(Seq.empty[Unit])
// This will only insert one at a time. You could use list.sliding to batch the
// inserts if that was important.
val temp1 = list.foldLeft(emptyFuture) { (previousFuture, trip) =>
previousFuture flatMap { previous =>
// Inner code copied from your example.
val data = for {
tripData <- TripDataRepository.insertQuery(trip.tripData)
subTripData <- SubTripDataRepository.insertBatchQuery(getUpdatedSubTripDataList(trip.subTripData, tripData.id))
} yield ((tripData, subTripData))
val res = db.run(data.transactionally)
previous :+ res
}
}

关于postgresql - 批量插入时 Slick 3.0 中的数据库异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30772853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com