Scala 斯卡拉杜比;Hikari CP事务处理程序

Scala 斯卡拉杜比;Hikari CP事务处理程序,scala,hikaricp,doobie,Scala,Hikaricp,Doobie,如果您这样做,HikariCP将初始化并每次关闭。 有没有办法避免这种情况并执行各种查询 // Resource yielding a transactor configured with a bounded connect EC and an unbounded // transaction EC. Everything will be closed and shut down cleanly after use. val transactor: Resource[IO, HikariTr

如果您这样做,HikariCP将初始化并每次关闭。 有没有办法避免这种情况并执行各种查询

// Resource yielding a transactor configured with a bounded connect EC and an unbounded
// transaction EC. Everything will be closed and shut down cleanly after use.
  val transactor: Resource[IO, HikariTransactor[IO]] =
  for {
    ce <- ExecutionContexts.fixedThreadPool[IO](32) // our connect EC
    be <- Blocker[IO] // our blocking EC
    xa <- HikariTransactor.newHikariTransactor[IO](
      "org.h2.Driver", // driver classname
      "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", // connect URL
      "sa", // username
      "", // password
      ce, // await connection here
      be // execute JDBC operations here
    )
  } yield xa

这不是在应用程序中使用
资源的方式

确实可以
。在
级别的某个位置使用
,然后让需要
事务处理程序
的整个代码传递该值,例如:

val actorSystemResource: Resource[IO, ActorSystem]
val transactorResource: Resource[IO, Transactor[IO]]

// initialize controllers, services, etc and create routes for them
def routes(actorSystem: ActorSystem, transactor: Transactor[IO]): Route

val resources = for {
  transactor <- transactorResource
  actorSystem, <- actorSystemResource
  route = routes(actorSystem, transactor)
} yield (transactor, actorSystem, route)

resources.use { case (_, actorSystem, route) =>
  implicit system = actorSystem

  IO.fromFuture {
    Http().bindAndHandle(route, "localhost", 8080)
  }
}
val actorSystemResource:Resource[IO,ActorSystem]
val事务处理程序资源:资源[IO,事务处理程序[IO]]
//初始化控制器、服务等并为其创建路由
def路由(actorSystem:actorSystem,Transactior:Transactior[IO]):路由
val资源=用于{
交易人
val actorSystemResource: Resource[IO, ActorSystem]
val transactorResource: Resource[IO, Transactor[IO]]

// initialize controllers, services, etc and create routes for them
def routes(actorSystem: ActorSystem, transactor: Transactor[IO]): Route

val resources = for {
  transactor <- transactorResource
  actorSystem, <- actorSystemResource
  route = routes(actorSystem, transactor)
} yield (transactor, actorSystem, route)

resources.use { case (_, actorSystem, route) =>
  implicit system = actorSystem

  IO.fromFuture {
    Http().bindAndHandle(route, "localhost", 8080)
  }
}