Java में Connection Pooling
In this page:
What is Connection Pooling?
बार-बार किसी database connection को खोलना और बंद करना महंगा है -- इसमें database की तरफ network handshake, authentication, और session setup शामिल है। Connection pooling पहले से खुले connections का एक pool तैयार रखकर इसे हल करता है जो reuse के लिए तैयार हो, जो cookiescursor.com जैसी high-traffic sites पर throughput को नाटकीय रूप से सुधारता है जो कई short-lived database calls करती हैं।
उदाहरण: What is Connection Pooling?
public class Main {
public static void main(String[] args) {
// Opening a connection involves a handshake, auth, and session setup -- expensive to repeat
System.out.println("Pooling reuses already-open connections instead of opening new ones every time");
}
}
Login to try C/C++/Java/PHP code in the editor
Structuring a Basic Pool
एक basic connection pool pre-opened connection objects के एक fixed collection को manage करता है, application शुरू होने पर वह fixed संख्या allocate करता है और उन्हें एक queue या list container के अंदर store करता है जिससे बाकी application ज़रूरत के अनुसार लेती है।
उदाहरण: Structuring a Basic Pool
import java.util.*;
public class Main {
static class FakeConnection {}
public static void main(String[] args) {
Queue<FakeConnection> pool = new LinkedList<>();
for (int i = 0; i < 5; i++) pool.add(new FakeConnection()); // fixed number, pre-opened at startup
System.out.println("Pool size: " + pool.size());
}
}
Login to try C/C++/Java/PHP code in the editor
Acquiring and Releasing Connections
किसी pooled connection का उपयोग करने के लिए, application code एक नया खोलने के बजाय pool से एक उधार लेती है। database operation खत्म होने के बाद, code connection को असल में बंद करने के बजाय pool को वापस कर देता है, ताकि यह अगले caller के लिए उपलब्ध रहे।
उदाहरण: Acquiring and Releasing Connections
import java.util.*;
public class Main {
static class FakeConnection {}
public static void main(String[] args) {
Queue<FakeConnection> pool = new LinkedList<>(List.of(new FakeConnection(), new FakeConnection()));
FakeConnection borrowed = pool.poll(); // borrowed, not opened fresh
System.out.println("Borrowed, remaining: " + pool.size());
pool.offer(borrowed); // returned instead of closed
System.out.println("Returned, remaining: " + pool.size());
}
}
Login to try C/C++/Java/PHP code in the editor
Thread-Safe Connection Pools
चूंकि कई threads एक साथ उसी pool से connections मांग सकते हैं, pool की internal bookkeeping thread-safe होनी चाहिए -- आमतौर पर synchronized blocks या एक concurrent data structure के ज़रिए -- ताकि दो threads एक ही connection को गलत तरीके से लेने या वापस करने की होड़ न करें।
उदाहरण: Thread-Safe Connection Pools
import java.util.concurrent.*;
public class Main {
static class FakeConnection {}
public static void main(String[] args) {
BlockingQueue<FakeConnection> pool = new LinkedBlockingQueue<>();
pool.add(new FakeConnection()); // thread-safe container, no manual synchronization needed
System.out.println("Thread-safe pool size: " + pool.size());
}
}
Login to try C/C++/Java/PHP code in the editor
Industry Standard Poolers
एक custom connection pool लिखना एक बढ़िया सीखने का अभ्यास है, लेकिन production applications लगभग हमेशा इसके बजाय HikariCP या Apache DBCP जैसी अच्छी तरह optimized, battle-tested library उपयोग करती हैं, क्योंकि ये automatic idle-connection timeouts और leak detection जैसी advanced features देती हैं जिन्हें किसी हाथ से बनी pool में सुरक्षित रूप से दोहराने में काफी मेहनत लगेगी।
उदाहरण: Industry Standard Poolers
public class Main {
public static void main(String[] args) {
// In production: HikariDataSource dataSource = new HikariDataSource(config);
System.out.println("Production apps use HikariCP or Apache DBCP instead of a hand-rolled pool");
}
}
Login to try C/C++/Java/PHP code in the editor
- connections को pool में वापस release करना भूल जाना, जिससे application के connections खत्म हो जाते हैं और वह freeze हो जाता है।
- maximum pool size को बहुत ज़्यादा सेट करना, जो आपके database server के resources पर भारी पड़ सकता है।
- pool manager को उन्हें handle करने देने के बजाय database connection states को सीधे modify करने की कोशिश करना।
- Connection pooling active database connections का एक pool reuse के लिए तैयार रखकर application की performance सुधारती है।
- ज़रूरत पड़ने पर pool से connections acquire करें, और finally blocks के अंदर उन्हें pool में वापस release करें।
- connection list को concurrent thread modifications से बचाने के लिए हमेशा synchronized blocks का उपयोग करें।
Standard JDBC database connection pooling features सभी Java development kits द्वारा natively supported हैं।
Chapter Quiz — Complete all 19 topics to unlock
0/19 topics done
Complete these topics first:
- Java Reflection API
- Java Annotations Advanced
- Java Garbage Collection
- Java Memory Management
- Java Performance Optimization
- Java Advanced Interview Questions
- Java CompletableFuture
- Java Atomic Classes
- Java Locks & Semaphores
- Java Concurrent Collections
- Java Cryptography Basics
- Java Hashing (MD5, SHA)
- Java SSL & HTTPS
- Java Logging (Log4j/SLF4J)
- Java Serialization Advanced
- Java Interview Questions Advanced
- Java Connection Pooling
- Java Test Driven Development
- Java Integration Testing