MySQLDataSource

ldbc.connector.MySQLDataSource
See theMySQLDataSource companion class

Companion object for MySQLDataSource providing factory methods.

Attributes

Companion
class
Source
MySQLDataSource.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def build[F[_] : UUIDGen](host: String, port: Int, user: String): MySQLDataSource[F, Unit]

Creates a MySQLDataSource with minimal required parameters.

Creates a MySQLDataSource with minimal required parameters.

This is a convenience factory method for creating a DataSource with just the essential connection parameters. Other settings will use their defaults.

Type parameters

F

the effect type with required type class instances

Value parameters

host

the hostname or IP address of the MySQL server

port

the port number on which the MySQL server is listening

user

the username for authenticating with the MySQL server

Attributes

Returns

a new MySQLDataSource with the specified parameters

Example
val dataSource = MySQLDataSource.build[IO](
 host = "localhost",
 port = 3306,
 user = "myuser"
)
Source
MySQLDataSource.scala
def default[F[_] : UUIDGen]: MySQLDataSource[F, Unit]

Creates a MySQLDataSource with default configuration.

Creates a MySQLDataSource with default configuration.

Uses the default MySQLConfig which connects to:

  • host: "127.0.0.1"
  • port: 3306
  • user: "root"
  • no password

Type parameters

F

the effect type with required type class instances

Attributes

Returns

a new MySQLDataSource with default settings

Source
MySQLDataSource.scala
def fromConfig[F[_] : UUIDGen](config: MySQLConfig): MySQLDataSource[F, Unit]

Creates a MySQLDataSource from a MySQLConfig instance.

Creates a MySQLDataSource from a MySQLConfig instance.

This factory method simplifies DataSource creation by using a pre-configured MySQLConfig object containing all connection parameters.

Type parameters

F

the effect type with required type class instances

Value parameters

config

the MySQLConfig containing connection parameters

Attributes

Returns

a new MySQLDataSource configured according to the provided config

Source
MySQLDataSource.scala
def pooling[F[_] : UUIDGen](config: MySQLConfig, logHandler: Option[LogHandler[F]], metricsTracker: Option[PoolMetricsTracker[F]]): Resource[F, PooledDataSource[F]]

Creates a pooled DataSource from a MySQL configuration.

Creates a pooled DataSource from a MySQL configuration.

This factory method creates a connection pool that manages multiple MySQL connections, providing efficient connection reuse and automatic connection lifecycle management. The pool implements various optimization strategies including:

  • Connection pooling with configurable min/max sizes
  • Automatic connection validation and health checks
  • Connection timeout and idle timeout management
  • Leak detection for connections not properly returned
  • Background maintenance tasks for pool health
  • Optional adaptive sizing based on load patterns
  • Comprehensive metrics tracking

The returned Resource ensures proper pool initialization and shutdown. When the resource is released, all connections are closed and background tasks are cancelled.

Type parameters

F

the effect type with required type class instances

Value parameters

config

the MySQL configuration containing pool settings

logHandler

optional handler for logging database operations

metricsTracker

optional tracker for pool metrics (defaults to in-memory)

Attributes

Returns

a Resource managing the pooled data source lifecycle

Example
val poolConfig = MySQLConfig.default
 .setHost("localhost")
 .setPort(3306)
 .setUser("myuser")
 .setPassword("mypassword")
 .setDatabase("mydb")
 .setMinConnections(5)
 .setMaxConnections(20)
 .setConnectionTimeout(30.seconds)
MySQLDataSource.pooling[IO](poolConfig).use { pool =>
 pool.getConnection.use { conn =>
   // Use connection
 }
}
Source
MySQLDataSource.scala
def poolingWithBeforeAfter[F[_] : UUIDGen, A](config: MySQLConfig, logHandler: Option[LogHandler[F]], metricsTracker: Option[PoolMetricsTracker[F]], before: Option[(Connection[F]) => F[A]], after: Option[(A, Connection[F]) => F[Unit]]): Resource[F, PooledDataSource[F]]

Creates a pooled DataSource with connection lifecycle hooks.

Creates a pooled DataSource with connection lifecycle hooks.

This variant of the pooling factory method allows you to specify callbacks that will be executed before and after each connection is acquired from the pool. This is particularly useful for:

  • Setting session-specific variables or configuration
  • Implementing connection-level auditing or logging
  • Preparing the connection state before use
  • Cleaning up resources after connection use
  • Implementing custom connection validation logic

The before hook is called after a connection is acquired from the pool but before it's returned to the client. The after hook is called when the connection is returned to the pool. These hooks are executed for every connection acquisition, not just when new connections are created.

Type parameters

A

the type returned by the before callback and passed to after

F

the effect type with required type class instances

Value parameters

after

optional callback executed when returning a connection to the pool

before

optional callback executed when acquiring a connection from the pool

config

the MySQL configuration containing pool settings

logHandler

optional handler for logging database operations

metricsTracker

optional tracker for pool metrics (defaults to in-memory)

Attributes

Returns

a Resource managing the pooled data source lifecycle

Example
case class SessionContext(userId: String, startTime: Long)
val beforeHook: Connection[IO] => IO[SessionContext] = conn =>
 for {
   _ <- conn.createStatement().flatMap(_.executeUpdate("SET SESSION sql_mode = 'STRICT_ALL_TABLES'"))
   startTime = System.currentTimeMillis
 } yield SessionContext("user123", startTime)
val afterHook: (SessionContext, Connection[IO]) => IO[Unit] = (ctx, conn) =>
 IO.println(s"Connection used by ${ctx.userId} for ${System.currentTimeMillis - ctx.startTime}ms")
MySQLDataSource.poolingWithBeforeAfter[IO, SessionContext](
 config = poolConfig,
 before = Some(beforeHook),
 after = Some(afterHook)
).use { pool =>
 pool.getConnection.use { conn =>
   // Connection has session variables set and usage will be logged
 }
}
Source
MySQLDataSource.scala