연결 풀링 및 트랜잭션
풀링으로 데이터베이스 연결을 효율적으로 관리하고 next.jdbc의 트랜잭션으로 데이터 무결성을 보장하는 방법을 배웁니다.
연결 풀링 및 트랜잭션은(는) CoddyKit의 무료 Clojure Functional Programming & JVM Backend Development 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Clojure Functional Programming & JVM Backend Development 강의 전체를 잠금 해제할 수 있습니다. Clojure Functional Programming & JVM Backend Development 강의에는 총 4개의 강의가 포함되어 있습니다.
“연결 풀링 및 트랜잭션”에서 뭘 배우나요?
풀링으로 데이터베이스 연결을 효율적으로 관리하고 next.jdbc의 트랜잭션으로 데이터 무결성을 보장하는 방법을 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Clojure Functional Programming & JVM Backend Development을(를) 배우며, 24/7 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 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.