doobie.hikari.HikariTransactor Scala Examples
The following examples show how to use doobie.hikari.HikariTransactor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: Doobie.scala From iotchain with MIT License | 5 votes |
package jbok.app.service.store.doobie import cats.effect.{Async, ContextShift, Resource} import doobie._ import doobie.hikari.HikariTransactor import jbok.core.config.DatabaseConfig object Doobie { def xa[F[_]](config: DatabaseConfig)(implicit F: Async[F], cs: ContextShift[F]): Resource[F, Transactor[F]] = for { ce <- ExecutionContexts.fixedThreadPool[F](32) // our connect EC te <- ExecutionContexts.cachedThreadPool[F] // our transaction EC xa <- HikariTransactor.newHikariTransactor[F]( config.driver, config.url, config.user, // username config.password, // password ce, // await connection here te // execute JDBC operations here ) } yield xa }
Example 2
Source File: FlywayMigrator.scala From zorechka-bot with MIT License | 5 votes |
package com.wix.zorechka.repos import com.zaxxer.hikari.HikariDataSource import doobie.hikari.HikariTransactor import org.flywaydb.core.Flyway import zio.console.{Console, putStrLn} import zio.{Task, ZIO} trait FlywayMigrator { val flywayMigrator: FlywayMigrator.Service } object FlywayMigrator { trait Service { def migrate(dbTransactor: HikariTransactor[Task]): ZIO[Console, Throwable, Unit] } trait Live extends FlywayMigrator { val flywayMigrator: Service = new Service { override def migrate(dbTransactor: HikariTransactor[Task]): ZIO[Console, Throwable, Unit] = for { _ <- putStrLn("Starting Flyway migration") _ <- dbTransactor.configure(dataSource => loadFlyWayAndMigrate(dataSource)) _ <- putStrLn("Finished Flyway migration") } yield () } private def loadFlyWayAndMigrate(dataSource: HikariDataSource) = ZIO.effect { Flyway.configure() .dataSource(dataSource) .load() .migrate() } } def migrate(dbTransactor: HikariTransactor[Task]): ZIO[FlywayMigrator with Console, Throwable, Unit] = ZIO.accessM[FlywayMigrator with Console](_.flywayMigrator.migrate(dbTransactor)) }
Example 3
Source File: DbTransactor.scala From zorechka-bot with MIT License | 5 votes |
package com.wix.zorechka.repos import cats.effect.Blocker import com.wix.zorechka.DbConfig import doobie.hikari.HikariTransactor import doobie.util.ExecutionContexts import zio.blocking.Blocking import zio.{Managed, Task} import zio.interop.catz._ object DbTransactor { def newMysqlTransactor(cfg: DbConfig)(implicit rt: zio.Runtime[Blocking]): Managed[Throwable, HikariTransactor[Task]] = { val res = for { connectEC <- ExecutionContexts.fixedThreadPool[Task](2) transactEC <- ExecutionContexts.cachedThreadPool[Task] xa <- HikariTransactor.newHikariTransactor[Task]( "com.mysql.jdbc.Driver", cfg.url, //s"jdbc:mysql://${dbConfig.host}:${config.port}/${config.mysql.schema}", cfg.username, cfg.password, connectEC, Blocker.liftExecutionContext(transactEC) ) } yield xa res.toManaged } }
Example 4
Source File: DB.scala From Learn-Scala-Programming with MIT License | 5 votes |
package ch14 import cats.effect.IO import doobie.hikari.HikariTransactor import org.flywaydb.core.Flyway object DB { def transactor(config: DBConfig): IO[HikariTransactor[IO]] = { HikariTransactor.newHikariTransactor[IO](config.driver, config.url, config.user, config.password) } def initialize(transactor: HikariTransactor[IO]): IO[Unit] = { transactor.configure { dataSource => IO { val flyWay = new Flyway() flyWay.setLocations("classpath:db_migrations") flyWay.setDataSource(dataSource) flyWay.migrate() } } } }
Example 5
Source File: Store.scala From docspell with GNU General Public License v3.0 | 5 votes |
package docspell.store import scala.concurrent.ExecutionContext import cats.effect._ import fs2._ import docspell.store.impl.StoreImpl import bitpeace.Bitpeace import doobie._ import doobie.hikari.HikariTransactor trait Store[F[_]] { def transact[A](prg: ConnectionIO[A]): F[A] def transact[A](prg: Stream[ConnectionIO, A]): Stream[F, A] def bitpeace: Bitpeace[F] def add(insert: ConnectionIO[Int], exists: ConnectionIO[Boolean]): F[AddResult] } object Store { def create[F[_]: Effect: ContextShift]( jdbc: JdbcConfig, connectEC: ExecutionContext, blocker: Blocker ): Resource[F, Store[F]] = { val hxa = HikariTransactor.newHikariTransactor[F]( jdbc.driverClass, jdbc.url.asString, jdbc.user, jdbc.password, connectEC, blocker ) for { xa <- hxa st = new StoreImpl[F](jdbc, xa) _ <- Resource.liftF(st.migrate) } yield st } }
Example 6
Source File: JDBCJournal.scala From zio-actors with Apache License 2.0 | 5 votes |
package zio.actors.persistence.jdbc import cats.effect.Blocker import com.zaxxer.hikari.HikariDataSource import doobie._ import doobie.hikari.HikariTransactor import doobie.implicits._ import zio.{ IO, Promise, Runtime, Task, UIO, ZIO } import zio.actors.ActorSystemUtils import zio.actors.persistence.PersistenceId.PersistenceId import zio.actors.persistence.journal.Journal import zio.actors.persistence.jdbc.JDBCConfig.DbConfig import zio.blocking.Blocking import zio.interop.catz._ private[actors] final class JDBCJournal[Ev](tnx: Transactor[Task]) extends Journal[Ev] { override def persistEvent(persistenceId: PersistenceId, event: Ev): Task[Unit] = for { bytes <- ActorSystemUtils.objToByteArray(event) _ <- SqlEvents.persistEvent(persistenceId, bytes).run.transact(tnx) } yield () override def getEvents(persistenceId: PersistenceId): Task[Seq[Ev]] = for { bytes <- SqlEvents.getEventsById(persistenceId).to[Seq].transact(tnx) events <- IO.collectAll(bytes.map(ActorSystemUtils.objFromByteArray(_).map(_.asInstanceOf[Ev]))) } yield events } private[actors] object JDBCJournal { private lazy val runtime = Runtime.default private lazy val transactorPromise = runtime.unsafeRun(Promise.make[Exception, HikariTransactor[Task]]) def getJournal[Ev](actorSystemName: String, configStr: String): Task[JDBCJournal[Ev]] = for { dbConfig <- JDBCConfig.getDbConfig(actorSystemName, configStr) tnx <- getTransactor(dbConfig) } yield new JDBCJournal[Ev](tnx) private def makeTransactor(dbConfig: DbConfig): ZIO[Blocking, Throwable, HikariTransactor[Task]] = ZIO.runtime[Blocking].flatMap { implicit rt => for { transactEC <- UIO(rt.environment.get.blockingExecutor.asEC) connectEC = rt.platform.executor.asEC ds = new HikariDataSource() _ = ds.setJdbcUrl(dbConfig.dbURL.value) _ = ds.setUsername(dbConfig.dbUser.value) _ = ds.setPassword(dbConfig.dbPass.value) transactor <- IO.effect(HikariTransactor.apply[Task](ds, connectEC, Blocker.liftExecutionContext(transactEC))) } yield transactor } private def getTransactor(dbConfig: DbConfig): Task[HikariTransactor[Task]] = transactorPromise.poll.flatMap { case Some(value) => value case None => for { newTnx <- makeTransactor(dbConfig).provideLayer(Blocking.live) _ <- transactorPromise.succeed(newTnx) } yield newTnx } }
Example 7
Source File: PostgresTransactor.scala From ticket-booking-aecor with Apache License 2.0 | 5 votes |
package ru.pavkin.booking.common.postgres import cats.effect.{Async, ContextShift, Resource} import doobie.hikari.HikariTransactor import doobie.util.ExecutionContexts import ru.pavkin.booking.config.PostgresConfig object PostgresTransactor { def transactor[F[_]]( config: PostgresConfig )(implicit F: Async[F], C: ContextShift[F]): Resource[F, HikariTransactor[F]] = for { ce <- ExecutionContexts.fixedThreadPool[F](32) te <- ExecutionContexts.cachedThreadPool[F] tr <- HikariTransactor.newHikariTransactor[F]( "org.postgresql.Driver", s"jdbc:postgresql://${config.contactPoints}:${config.port}/${config.database}", config.username, config.password, ce, te ) _ <- Resource.liftF(tr.configure(ds => F.delay(ds.setAutoCommit(false)))) } yield tr }
Example 8
Source File: DatabaseConfig.scala From scala-pet-store with Apache License 2.0 | 5 votes |
package io.github.pauljamescleary.petstore.config import cats.syntax.functor._ import cats.effect.{Async, Blocker, ContextShift, Resource, Sync} import doobie.hikari.HikariTransactor import org.flywaydb.core.Flyway import scala.concurrent.ExecutionContext case class DatabaseConnectionsConfig(poolSize: Int) case class DatabaseConfig( url: String, driver: String, user: String, password: String, connections: DatabaseConnectionsConfig, ) object DatabaseConfig { def dbTransactor[F[_]: Async: ContextShift]( dbc: DatabaseConfig, connEc: ExecutionContext, blocker: Blocker, ): Resource[F, HikariTransactor[F]] = HikariTransactor .newHikariTransactor[F](dbc.driver, dbc.url, dbc.user, dbc.password, connEc, blocker) def initializeDb[F[_]](cfg: DatabaseConfig)(implicit S: Sync[F]): F[Unit] = S.delay { val fw: Flyway = { Flyway .configure() .dataSource(cfg.url, cfg.user, cfg.password) .load() } fw.migrate() } .as(()) }
Example 9
package tamer package db import java.sql.SQLException import java.time.Instant import cats.effect.Blocker import doobie.hikari.HikariTransactor import doobie.implicits._ import doobie.util.transactor.Transactor import eu.timepit.refined.auto._ import fs2.{Chunk, Stream} import log.effect.LogWriter import log.effect.zio.ZioLogWriter.log4sFromName import tamer.config.{DbConfig, QueryConfig} import zio._ import zio.interop.catz._ import scala.concurrent.ExecutionContext trait Db extends Serializable { val db: Db.Service[Any] } object Db { implicit class InstantOps(ours: Instant) { def -(theirs: Instant): Long = ours.toEpochMilli - theirs.toEpochMilli } case class ChunkWithMetadata[V](chunk: Chunk[V], pulledAt: Instant = Instant.now()) case class ValueWithMetadata[V](value: V, pulledAt: Instant = Instant.now()) trait Service[R] { def runQuery[K, V, State]( tnx: Transactor[Task], setup: Setup[K, V, State], queryConfig: QueryConfig )(state: State, q: Queue[(K, V)]): ZIO[R, DbError, State] } object > extends Service[Db] { override final def runQuery[K, V, State]( tnx: Transactor[Task], setup: Setup[K, V, State], queryConfig: QueryConfig )(state: State, q: Queue[(K, V)]): ZIO[Db, DbError, State] = ZIO.accessM(_.db.runQuery(tnx, setup, queryConfig)(state, q)) } trait Live extends Db { override final val db: Service[Any] = new Service[Any] { private[this] val logTask: Task[LogWriter[Task]] = log4sFromName.provide("tamer.Db.Live") override final def runQuery[K, V, State]( tnx: Transactor[Task], setup: Setup[K, V, State], queryConfig: QueryConfig )(state: State, q: Queue[(K, V)]): IO[DbError, State] = (for { log <- logTask query <- UIO(setup.buildQuery(state)) _ <- log.debug(s"running ${query.sql} with params derived from $state").ignore start <- UIO(Instant.now()) values <- query .streamWithChunkSize(queryConfig.fetchChunkSize) .chunks .transact(tnx) .map(c => ChunkWithMetadata(c)) .evalTap(c => q.offerAll(c.chunk.iterator.to(LazyList).map(v => setup.valueToKey(v) -> v))) .flatMap(c => Stream.chunk(c.chunk).map(v => ValueWithMetadata(v, c.pulledAt))) .compile .toList newState <- setup.stateFoldM(state)( QueryResult( ResultMetadata(values.headOption.fold(Instant.now())(_.pulledAt) - start), values.map(_.value) ) ) } yield newState).mapError { case e: Exception => DbError(e.getLocalizedMessage) } } } def mkTransactor(db: DbConfig, connectEC: ExecutionContext, transactEC: ExecutionContext): Managed[DbError, HikariTransactor[Task]] = Managed { HikariTransactor .newHikariTransactor[Task](db.driver, db.uri, db.username, db.password, connectEC, Blocker.liftExecutionContext(transactEC)) .allocated .map { case (ht, cleanup) => Reservation(ZIO.succeed(ht), _ => cleanup.orDie) } .uninterruptible .refineToOrDie[SQLException] .mapError(sqle => DbError(sqle.getLocalizedMessage())) } }
Example 10
Source File: DoobieIOApp.scala From advanced-scala-code with Apache License 2.0 | 5 votes |
import cats.effect._ import cats.implicits._ import doobie.implicits._ import doobie.hikari.HikariTransactor import doobie.util.ExecutionContexts object DoobieIOApp extends IOApp { override def run(args: List[String]): IO[ExitCode] = { // Resource[IO, HikariTransactor[IO]] val transactor: Resource[IO, HikariTransactor[IO]] = for { ce <- ExecutionContexts.fixedThreadPool[IO](32) te <- ExecutionContexts.cachedThreadPool[IO] xa <- HikariTransactor.newHikariTransactor[IO]( "org.postgresql.Driver", "jdbc:postgresql:doobieworld", "doobieuser", "doobiepass", ce, te) } yield xa val districtC = sql"select district from city where name = 'Canberra'". query[String].unique transactor.use { xa => for { district <- districtC.transact(xa) _ <- IO(println(district)) } yield ExitCode.Success } } }
Example 11
Source File: MySqlInvoiceList.scala From event-sourcing-kafka-streams with MIT License | 5 votes |
package org.amitayh.invoices.dao import cats.Monad import cats.effect.{Async, ContextShift, Resource} import cats.syntax.functor._ import doobie.free.connection.ConnectionIO import doobie.hikari.HikariTransactor import doobie.implicits._ import doobie.util.ExecutionContexts import doobie.util.transactor.Transactor class MySqlInvoiceList[F[_]: Monad](transactor: Transactor[F]) extends InvoiceList[F] { override def save(record: InvoiceRecord): F[Unit] = MySqlInvoiceList.save(record).transact(transactor) override def get: F[List[InvoiceRecord]] = MySqlInvoiceList.get.transact(transactor) } object MySqlInvoiceList { def save(record: InvoiceRecord): ConnectionIO[Unit] = { import record._ val sql = sql""" INSERT INTO invoices (id, version, updated_at, customer_name, customer_email, issue_date, due_date, total, status) VALUES ($id, $version, $updatedAt, $customerName, $customerEmail, $issueDate, $dueDate, $total, $status) ON DUPLICATE KEY UPDATE version = VALUES(version), updated_at = VALUES(updated_at), customer_name = VALUES(customer_name), customer_email = VALUES(customer_email), issue_date = VALUES(issue_date), due_date = VALUES(due_date), total = VALUES(total), status = VALUES(status) """ sql.update.run.void } def get: ConnectionIO[List[InvoiceRecord]] = { val sql = sql""" SELECT id, version, updated_at, customer_name, customer_email, issue_date, due_date, total, status FROM invoices WHERE status IN ('New', 'Paid') ORDER BY created_at DESC """ sql.query[InvoiceRecord].to[List] } def resource[F[_]: Async: ContextShift]: Resource[F, MySqlInvoiceList[F]] = for { connectEC <- ExecutionContexts.fixedThreadPool[F](32) transactEC <- ExecutionContexts.cachedThreadPool[F] transactor <- HikariTransactor.newHikariTransactor[F]( driverClassName = sys.env("DB_DRIVER"), url = sys.env("DB_URL"), user = sys.env("DB_USER"), pass = sys.env("DB_PASS"), connectEC = connectEC, transactEC = transactEC) } yield new MySqlInvoiceList[F](transactor) }
Example 12
Source File: DoobieHikariModule.scala From scala-server-toolkit with MIT License | 5 votes |
package com.avast.sst.doobie import java.util.Properties import java.util.concurrent.{ScheduledExecutorService, ThreadFactory} import cats.Show import cats.effect.{Async, Blocker, ContextShift, Resource, Sync} import cats.syntax.show._ import com.zaxxer.hikari.HikariConfig import com.zaxxer.hikari.metrics.MetricsTrackerFactory import doobie.enum.TransactionIsolation import doobie.hikari.HikariTransactor import scala.concurrent.ExecutionContext object DoobieHikariModule { def make[F[_]: Async]( config: DoobieHikariConfig, boundedConnectExecutionContext: ExecutionContext, blocker: Blocker, metricsTrackerFactory: Option[MetricsTrackerFactory] = None )(implicit cs: ContextShift[F]): Resource[F, HikariTransactor[F]] = { for { hikariConfig <- Resource.liftF(makeHikariConfig(config, metricsTrackerFactory)) transactor <- HikariTransactor.fromHikariConfig(hikariConfig, boundedConnectExecutionContext, blocker) } yield transactor } implicit private val transactionIsolationShow: Show[TransactionIsolation] = { case TransactionIsolation.TransactionNone => "TRANSACTION_NONE" case TransactionIsolation.TransactionReadUncommitted => "TRANSACTION_READ_UNCOMMITTED" case TransactionIsolation.TransactionReadCommitted => "TRANSACTION_READ_COMMITTED" case TransactionIsolation.TransactionRepeatableRead => "TRANSACTION_REPEATABLE_READ" case TransactionIsolation.TransactionSerializable => "TRANSACTION_SERIALIZABLE" } private def makeHikariConfig[F[_]: Sync]( config: DoobieHikariConfig, metricsTrackerFactory: Option[MetricsTrackerFactory], scheduledExecutorService: Option[ScheduledExecutorService] = None, threadFactory: Option[ThreadFactory] = None ): F[HikariConfig] = { Sync[F].delay { val c = new HikariConfig() c.setDriverClassName(config.driver) c.setJdbcUrl(config.url) c.setUsername(config.username) c.setPassword(config.password) c.setAutoCommit(config.autoCommit) c.setConnectionTimeout(config.connectionTimeout.toMillis) c.setIdleTimeout(config.idleTimeout.toMillis) c.setMaxLifetime(config.maxLifeTime.toMillis) c.setMinimumIdle(config.minimumIdle) c.setMaximumPoolSize(config.maximumPoolSize) c.setReadOnly(config.readOnly) c.setAllowPoolSuspension(config.allowPoolSuspension) c.setIsolateInternalQueries(config.isolateInternalQueries) c.setRegisterMbeans(config.registerMBeans) val dataSourceProperties = new Properties() config.dataSourceProperties.foreach { case (k, v) => dataSourceProperties.put(k, v) } c.setDataSourceProperties(dataSourceProperties) config.leakDetectionThreshold.map(_.toMillis).foreach(c.setLeakDetectionThreshold) config.initializationFailTimeout.map(_.toMillis).foreach(c.setInitializationFailTimeout) config.poolName.foreach(c.setPoolName) config.validationTimeout.map(_.toMillis).foreach(c.setValidationTimeout) config.transactionIsolation.map(_.show).foreach(c.setTransactionIsolation) scheduledExecutorService.foreach(c.setScheduledExecutor) threadFactory.foreach(c.setThreadFactory) metricsTrackerFactory.foreach(c.setMetricsTrackerFactory) c } } }