コネクションプーリングとトランザクション
コネクションプーリングでデータベース接続を効率的に管理し、next.jdbcのトランザクションでデータの整合性を保証します。
「コネクションプーリングとトランザクション」はCoddyKit上の無料Clojure Functional Programming & JVM Backend Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 backIsolation 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/->pooland HikariCP - Wrap atomic writes in
with-transaction - Failures roll back automatically; choose isolation levels for concurrency
よくある質問
「コネクションプーリングとトランザクション」レッスンは無料ですか?
はい。「コネクションプーリングとトランザクション」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Clojure Functional Programming & JVM Backend Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Clojure Functional Programming & JVM Backend Developmentコースには全4レッスンが含まれています。
「コネクションプーリングとトランザクション」で何を学びますか?
コネクションプーリングでデータベース接続を効率的に管理し、next.jdbcのトランザクションでデータの整合性を保証します。 ブラウザで直接実行するハンズオンコードでClojure Functional Programming & JVM Backend Developmentを演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- next.jdbcによるデータベース接続
- CRUD操作の実行
- データベースマイグレーションとスキーマ管理
- コネクションプーリングとトランザクション