การจัดกลุ่มการเชื่อมต่อและธุรกรรม
จัดการการเชื่อมต่อฐานข้อมูลอย่างมีประสิทธิภาพด้วยการจัดกลุ่ม และรักษาความถูกต้องของข้อมูลด้วยธุรกรรมใน next.jdbc
การจัดกลุ่มการเชื่อมต่อและธุรกรรม เป็นบทเรียน Clojure Functional Programming & JVM Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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
คำถามที่พบบ่อย
บทเรียน “การจัดกลุ่มการเชื่อมต่อและธุรกรรม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดกลุ่มการเชื่อมต่อและธุรกรรม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Clojure Functional Programming & JVM Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Clojure Functional Programming & JVM Backend Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดกลุ่มการเชื่อมต่อและธุรกรรม”
จัดการการเชื่อมต่อฐานข้อมูลอย่างมีประสิทธิภาพด้วยการจัดกลุ่ม และรักษาความถูกต้องของข้อมูลด้วยธุรกรรมใน next.jdbc คุณปฏิบัติ Clojure Functional Programming & JVM Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Clojure Functional Programming & JVM Backend Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Clojure Functional Programming & JVM Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดกลุ่มการเชื่อมต่อและธุรกรรม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Clojure Functional Programming & JVM Backend Development นี้ได้ไหม
ได้ บทเรียน Clojure Functional Programming & JVM Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเชื่อมต่อฐานข้อมูลด้วย next.jdbc
- การดำเนินการสร้าง อ่าน ปรับปรุง และลบข้อมูล
- การย้ายฐานข้อมูลและการจัดการสคีมา
- การจัดกลุ่มการเชื่อมต่อและธุรกรรม