0Pricing
Clojure Functional Programming & JVM Backend Development · 课时

连接池与事务

使用连接池高效管理数据库连接,并在 next.jdbc 中通过事务确保数据完整性。

连接池与事务 是 CoddyKit 上的免费 Clojure Functional Programming & JVM Backend Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Clojure Functional Programming & JVM Backend Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Clojure Functional Programming & JVM Backend Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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

常见问题解答

「连接池与事务」课时是免费的吗?

是的 — 「连接池与事务」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Clojure Functional Programming & JVM Backend Development 课程的其余内容,请升级到 CoddyKit PRO。 Clojure Functional Programming & JVM Backend Development 课程共包含 4 节课。

「连接池与事务」这节课中我会学到什么?

使用连接池高效管理数据库连接,并在 next.jdbc 中通过事务确保数据完整性。 你通过在浏览器中直接运行的动手代码来练习 Clojure Functional Programming & JVM Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Clojure Functional Programming & JVM Backend Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Clojure Functional Programming & JVM Backend Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「连接池与事务」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Clojure Functional Programming & JVM Backend Development 课中编写并运行代码吗?

能。每节 Clojure Functional Programming & JVM Backend Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 next.jdbc 连接数据库
  2. 执行 CRUD 操作
  3. 数据库迁移与架构管理
  4. 连接池与事务
← 返回 Clojure Functional Programming & JVM Backend Development