DataSource

ldbc.DataSource
trait DataSource[F[_]]

A factory for database connections that provides connections as managed resources.

DataSource is a fundamental abstraction in ldbc that encapsulates the logic for establishing database connections. It provides a uniform interface for obtaining connections regardless of the underlying implementation (JDBC, native MySQL protocol, etc.).

Implementations of this trait are responsible for:

  • Connection pooling (if applicable)
  • Connection configuration and initialization
  • Resource lifecycle management
  • Error handling during connection establishment

The connections are provided as cats.effect.Resource instances, ensuring that:

  • Connections are properly initialized before use
  • Resources are cleaned up when no longer needed
  • Connection leaks are prevented through automatic resource management

Type parameters

F

the effect type (e.g., IO, Future, etc.) that wraps the operations

Attributes

See also

ldbc.connector.MySQLDataSource for the pure Scala implementation

jdbc.connector.MySQLDataSource for the JDBC-based implementation

Example
// Using a DataSource to execute database operations
val dataSource: DataSource[IO] = MySQLDataSource.fromConfig(config)
val result: IO[List[User]] = dataSource.getConnection.use { connection =>
 sql"SELECT * FROM users".query[User].to[List].run(connection)
}
Source
DataSource.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Abstract methods

Creates a new database connection wrapped in a Resource.

Creates a new database connection wrapped in a Resource.

The returned Resource ensures that the connection is properly:

  • Acquired: Connection establishment, authentication, and initialization
  • Used: The connection is ready for executing SQL statements
  • Released: Connection cleanup, closing network resources, etc.

Each call to this method may return a new connection or a pooled connection, depending on the implementation. Users should not make assumptions about connection identity or state between calls.

Attributes

Returns

a Resource that manages the lifecycle of a database connection

Note

The connection is only valid within the Resource's use block. Attempting to use it outside will result in errors.

Source
DataSource.scala