0Pricing
Clojure Functional Programming & JVM Backend Development · Lesson

Connection Pooling & Transactions

Manage database connections efficiently with pooling and ensure data integrity using transactions in next.jdbc.

Connection Pooling & Transactions is a free Clojure Functional Programming & JVM Backend Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Clojure Functional Programming & JVM Backend Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Connection Pooling?

Opening a new database connection for every query is slow and resource-heavy. A connection pool keeps a set of reusable connections ready, dramatically improving throughput.

The HikariCP Pool

The most popular JVM connection pool is HikariCP. The next.jdbc.connection namespace provides a helper to build a pooled datasource.

(require '[next.jdbc.connection :as connection])
(import '[com.zaxxer.hikari HikariDataSource])

Creating a Pooled Datasource

Use connection/->pool with the pool class and your db spec. The returned datasource is what you query against.

(def datasource
  (connection/->pool HikariDataSource
    {:dbtype "postgresql"
     :dbname "mydb"
     :username "app"
     :password "secret"}))

Querying Through the Pool

Once created, pass the pooled datasource directly to jdbc/execute! just like a plain connection. The pool checks a connection out and back automatically.

(require '[next.jdbc :as jdbc])

(jdbc/execute! datasource
  ["SELECT * FROM users WHERE active = ?" true])

Closing the Pool

A pooled datasource is a closeable resource. Close it on shutdown to release all connections.

(.close datasource)

What Is a Transaction?

A transaction groups several statements so they all succeed or all fail together. This guarantees consistency, like transferring money between accounts.

with-transaction

next.jdbc provides with-transaction. Statements inside the body run atomically; if an exception is thrown, everything rolls back.

(jdbc/with-transaction [tx datasource]
  (jdbc/execute! tx ["UPDATE acct SET bal = bal - 100 WHERE id = 1"])
  (jdbc/execute! tx ["UPDATE acct SET bal = bal + 100 WHERE id = 2"]))

Automatic Rollback

If any statement throws, the transaction is rolled back automatically and the exception propagates. No partial updates are committed.

(jdbc/with-transaction [tx datasource]
  (jdbc/execute! tx ["INSERT INTO orders (id) VALUES (1)"])
  (throw (ex-info "boom" {}))) ; insert above is rolled back

Isolation Levels

You can specify an isolation level to control how concurrent transactions see each other.

  • :read-committed
  • :repeatable-read
  • :serializable
(jdbc/with-transaction [tx datasource {:isolation :serializable}]
  (jdbc/execute! tx ["UPDATE counters SET n = n + 1 WHERE id = 1"]))

Manual Rollback

You can force a rollback without throwing by setting the rollback flag on the connection.

(jdbc/with-transaction [tx datasource]
  (jdbc/execute! tx ["INSERT INTO logs (msg) VALUES ('test')"])
  (.rollback tx))

Pooling + Transactions Together

In production you create one pooled datasource at startup and wrap critical multi-step writes in with-transaction. This gives both performance and integrity.

(defn transfer! [ds from to amount]
  (jdbc/with-transaction [tx ds]
    (jdbc/execute! tx ["UPDATE acct SET bal = bal - ? WHERE id = ?" amount from])
    (jdbc/execute! tx ["UPDATE acct SET bal = bal + ? WHERE id = ?" amount to])))

Quick Check

Test your transaction knowledge.

Recap

You learned to use connection pooling and transactions with next.jdbc.

  • Build a pool with connection/->pool and HikariCP
  • Wrap atomic writes in with-transaction
  • Failures roll back automatically; choose isolation levels for concurrency

Frequently asked questions

Is the “Connection Pooling & Transactions” lesson free?

Yes — the full text of “Connection Pooling & Transactions” is free to read here on the web, and the Clojure Functional Programming & JVM Backend Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Clojure Functional Programming & JVM Backend Development course, upgrade to CoddyKit PRO.

What will I learn in “Connection Pooling & Transactions”?

Manage database connections efficiently with pooling and ensure data integrity using transactions in next.jdbc. You practise Clojure Functional Programming & JVM Backend Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Clojure Functional Programming & JVM Backend Development?

No prior experience is required. Clojure Functional Programming & JVM Backend Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Connection Pooling & Transactions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Clojure Functional Programming & JVM Backend Development lesson?

Yes. Every Clojure Functional Programming & JVM Backend Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Connecting to Databases with next.jdbc
  2. Performing CRUD Operations
  3. Database Migrations & Schema Management
  4. Connection Pooling & Transactions
← Back to Clojure Functional Programming & JVM Backend Development