A DataSource implementation for MySQL connections using the pure Scala MySQL wire protocol.
This DataSource provides connection pooling capabilities and manages MySQL connections with support for SSL, authentication, prepared statements, and various connection options. It also supports lifecycle hooks that can be executed before and after connection acquisition.
Type parameters
A
the type of value returned by the before hook
F
the effect type (e.g., IO, Task) that must have Async, Network, Console, Hashing, and UUIDGen capabilities
Value parameters
after
optional hook to execute after a connection is used
allowPublicKeyRetrieval
whether to allow retrieval of RSA public keys from the server
before
optional hook to execute before a connection is acquired
database
the default database to use upon connection
databaseTerm
the database terminology to use (CATALOG or SCHEMA)
debug
whether to enable debug logging for connections
host
the hostname or IP address of the MySQL server
logHandler
optional handler for logging connection and query events
password
the password for authenticating with the MySQL server
port
the port number on which the MySQL server is listening
readTimeout
the timeout duration for read operations
socketOptions
socket-level options for the TCP connection
ssl
the SSL configuration for secure connections
tracer
optional OpenTelemetry tracer for distributed tracing
useCursorFetch
whether to use cursor-based fetching for result sets
useServerPrepStmts
whether to use server-side prepared statements
user
the username for authenticating with the MySQL server
Attributes
Example
val dataSource = MySQLDataSource[IO, Unit](
host = "localhost",
port = 3306,
user = "myuser",
password = Some("mypassword"),
database = Some("mydatabase"),
ssl = SSL.Trusted
)
// With lifecycle hooks
val dataSourceWithHooks = dataSource
.withBefore(conn => IO.println("Connection acquired"))
.withAfter((_, conn) => IO.println("Connection released"))
Creates a new connection resource from this DataSource.
Creates a new connection resource from this DataSource.
The connection is managed as a resource, ensuring proper cleanup when the resource is released. If before/after hooks are configured, they will be executed during the connection lifecycle.
Adds both before and after hooks for connection lifecycle management.
Adds both before and after hooks for connection lifecycle management.
This is a convenience method that combines withBefore and withAfter, allowing you to set up both hooks in a single call. The before hook is executed when a connection is acquired, and the after hook is executed when it's released.
Type parameters
B
the type of value returned by the before hook
Value parameters
after
the function to execute after using a connection
before
the function to execute before using a connection
Attributes
Returns
a new MySQLDataSource with both hooks configured
Example
val dataSourceWithHooks = dataSource.withBeforeAfter(
before = conn => IO.println("Starting transaction").as(System.currentTimeMillis),
after = (startTime, conn) => IO.println(s"Completed in ${System.currentTimeMillis - startTime}ms")
)