简体   繁体   中英

How can I make an implicit field, transient?

I want to set up doobie inside the AsyncFunction of apache Flink but it needs an implicit read instance of output. on the other hand, I can not send Read[Out] as an implicit parameter because the AsyncFunction needs to be serializable and this implicit value violates this rule:

package FlinkHelpers
import Configs.JdbConfig
import doobie.hikari._
import doobie._
import doobie.implicits._
import doobie.util.ExecutionContexts
import cats._
import cats.data._
import cats.effect.IO.asyncForIO
import cats.effect._
import cats.implicits._
import fs2.Stream
import cats.effect.unsafe.implicits.global
import org.apache.flink.runtime.concurrent.Executors
import org.apache.flink.streaming.api.scala.async.{AsyncFunction, ResultFuture}

import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try
class AsyncDatabaseRequestOne[In,Out](configs:JdbConfig)(implicit r:Read[Out]) extends AsyncFunction[In,(In, Out)] {
  lazy val transactor: Resource[IO, HikariTransactor[IO]] = {
    for {
      ce <- ExecutionContexts.fixedThreadPool[IO](32)
      xa <- HikariTransactor.newHikariTransactor[IO](
        "org.postgresql.Driver",
        "jdbc:postgresql://localhost:5433/doobie",
        "psql",
        "110271",
        ce
      )
    } yield xa
  }
  /** The context used for the future callbacks */
  implicit lazy val executor: ExecutionContext = ExecutionContext.fromExecutor(Executors.directExecutor())

  override def asyncInvoke(input: In, resultFuture: ResultFuture[(In, Out)]): Unit = {
    val v2: IO[Out] = transactor.use[Out] { xa2: HikariTransactor[IO] => {
      sql"select code,name,population,gnp from Out limit 1"
        .query[Out] // Query0[String]
        .unique // ConnectionIO[List[String]]
        .transact(xa2) // IO[List[String]]
    }
    }
    val future = v2.unsafeToFuture()
    future.onSuccess({
      case result: Out => resultFuture.complete(List((input, result)))
    })
  }
}

on the other hand scala does not allow me to make the r field transiend does anyone have any idea how I can solve this?

r in class AsyncDatabaseRequestOne[In,Out](configs:JdbConfig)(r:Read[Out])... is a constructor parameter but Scala can generate private[this] field for it too:

Disambiguate constructor parameter with same name as class field of superclass ( answer )

Try to add annotation @transient

class AsyncDatabaseRequestOne[In,Out](configs:JdbConfig)(@transient private[this] implicit val r:Read[Out])...

or

class AsyncDatabaseRequestOne[In,Out](configs:JdbConfig)(@(transient @field) private[this] implicit val r:Read[Out])...

Since @transient is defined already with @field

@field
final class transient extends scala.annotation.StaticAnnotation

I guess in our case @field meta-annotation is not necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM