Java Database Connectivity (JDBC) is the official Java API, which defines how a client may access DB. This is generic API for all kinds of RDBMS.
JDBC provides generic way how to use DriverManager
to deal in DB-agnostic way with statements and result
sets.
JDBC provides a means for application to send SQL query to DB server, get back result and map the results to known primitive data types.
When application wants to talk with DB, it opens a new connection and, in scope of connection, application prepares data, statements etc, opens and closes transactions, executes queries, gets result set, parses it and, when done, closes connection.
In case of some issues, JDBC throws one of SQLException
s and it is up to application to deal with
consequences.
For large and long-living applications, DB connections are maintained by special DB connection pools. It is done to speed up opening of connection and to automate correct resource handling.
More information:
- official documentation, JDBC 4.2
- high level overview in Java
- Structured Query Language (SQL)
- SQL tutorial
doobie
is a functional library to make work with DB/SQL simpler, safer and effectful.
From the Book:
doobie
programs are values. You can compose small programs to build larger programs. Once you have constructed a program you wish to run, you interpret it into an effectful target monad of your choice (IO for example) and drop it into your main application wherever you like.
Basic building blocks:
sql"..."
,fr0"..."
andfr"..."
- complete query "fragment" with ability to interpolate variables, similar tos"..."
Get
,Put
,Meta
,Read
,Write
- implicit mappers between JDBC and Scala typestransactor
- SQL statement interpreter
doobie
programs usually are ConnectionIO[A]
which can be computed in context of java.sql.Connection
and
which return value of type A
as a result (or fails with an error).
Usually doobie
programs are interpreted by transactor
, which takes care of:
- providing
Connection
- wrapping calls in SQL transactions (customizable)
- making rollbacks on errors
- cleanup, like closing of all opened resources after query is done
- returns "normal" effectful program, usually, some kind of
IO
In nutshell, transactor
provides a way how to transform ConnectionIO[A]
to IO[A]
.
More information:
From official documentation:
Slick (“Scala Language-Integrated Connection Kit”) is Lightbend’s Functional Relational Mapping (FRM) library for Scala that makes it easy to work with relational databases.
If we will have some free time, we can talk about Slick.
It is Future
base, not IO
based, while Slick's DBIO[A]
is very similar to doobie
's ConnectionIO[A]
.
More information: