Connection Pooling und Transaktionen
Verwalten Sie Datenbankverbindungen mit Connection Pooling effizient und stellen Sie die Datenintegrität mit Transaktionen in next.jdbc sicher.
Connection Pooling und Transaktionen ist eine kostenlose Clojure Functional Programming & JVM Backend Development-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Clojure Functional Programming & JVM Backend Development-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Clojure Functional Programming & JVM Backend Development-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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
Häufig gestellte Fragen
Ist die Lektion „Connection Pooling und Transaktionen“ kostenlos?
Ja — der vollständige Text von „Connection Pooling und Transaktionen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Clojure Functional Programming & JVM Backend Development-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Clojure Functional Programming & JVM Backend Development-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Connection Pooling und Transaktionen“?
Verwalten Sie Datenbankverbindungen mit Connection Pooling effizient und stellen Sie die Datenintegrität mit Transaktionen in next.jdbc sicher. Du übst Clojure Functional Programming & JVM Backend Development mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Clojure Functional Programming & JVM Backend Development zu starten?
Keine Vorkenntnisse erforderlich. Clojure Functional Programming & JVM Backend Development auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Connection Pooling und Transaktionen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Clojure Functional Programming & JVM Backend Development-Lektion Code schreiben und ausführen?
Ja. Jede Clojure Functional Programming & JVM Backend Development-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Mit next.jdbc eine Verbindung zu Datenbanken herstellen
- CRUD-Operationen durchführen
- Datenbankmigrationen und Schemaverwaltung
- Connection Pooling und Transaktionen