← Back to Advanced Java Course | Chapter 11: Advanced & Security | Lesson 17 of 19

Java में Connection Pooling

एक connection pool reuse के लिए तैयार database connections का एक set रखता है, जैसे shared bikes का एक rack। यह हर बार एक नया connection बनाने से कहीं तेज़ है।

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?

java
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");
	}
}

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

java
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());
	}
}

Acquiring and Releasing Connections

किसी pooled connection का उपयोग करने के लिए, application code एक नया खोलने के बजाय pool से एक उधार लेती है। database operation खत्म होने के बाद, code connection को असल में बंद करने के बजाय pool को वापस कर देता है, ताकि यह अगले caller के लिए उपलब्ध रहे।

उदाहरण: Acquiring and Releasing Connections

java
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());
	}
}

Thread-Safe Connection Pools

चूंकि कई threads एक साथ उसी pool से connections मांग सकते हैं, pool की internal bookkeeping thread-safe होनी चाहिए -- आमतौर पर synchronized blocks या एक concurrent data structure के ज़रिए -- ताकि दो threads एक ही connection को गलत तरीके से लेने या वापस करने की होड़ न करें।

उदाहरण: Thread-Safe Connection Pools

java
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());
	}
}

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

java
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");
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. connections को pool में वापस release करना भूल जाना, जिससे application के connections खत्म हो जाते हैं और वह freeze हो जाता है।
  2. maximum pool size को बहुत ज़्यादा सेट करना, जो आपके database server के resources पर भारी पड़ सकता है।
  3. 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 हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.