Design a Thread Safe Connection Pool - Object Oriented Design Interview Preparation - Java

preview_player
Показать описание
Design and implement a Thread Safe Connection Pool

#objectorienteddesign #objectorientedprogramming #systemdesigninterview #systemdesign #coding #codinginterviewprep #java #databaseconnectivity
#faangcodinginterviewpreparation

Implemented a Connection Pool using an interface called ConnectionPool.

The ConnectionPool Interface has methods like: getConnection(long timeout) and releaseConnection(Connection connection)

In this Video, I have two implementations of Connection Pool.
First implemented the ConnectionPool using LinkedList as Queue
and then implemented using an ArrayBlockingQueue

Wrote a ConnectionPoolTest class to test both implementations using 100 maxPoolSize and firing 1000 Mysql Queries using Java Virtual Thread Per Task Executor.

Timestamps:
0:00 Introduction
4:00 Implementation using LinkedList as Queue
10:24 Implementation using an ArrayBlockingQueue
13:33 Test class
19:50 Running and the testing the LinkedList implementation Connection Pool
22:34 Running and the testing the BlockingQueue implementation Connection Pool

Why Blocking Queue?
The key advantage of BlockingQueue is that it has built-in thread safety and blocking mechanisms. This means that we don't have to manually handle the waiting and notification of threads when acquiring or releasing connections.

The BlockingQueue Methods make the code simpler, more readable, and less prone to errors associated with manual synchronization.

What Poll(timeout) does?

The 'poll()' method with a timeout is used when you want to acquire a connection from the pool but don't want to wait indefinitely if no connections are available. The timeout parameter specifies the maximum time the method will block, waiting for a connection to become available. If a connection is not available within the specified timeout, the method returns null, indicating that the connection acquisition attempt failed.

Рекомендации по теме
Комментарии
Автор

Great and informative content . Love it when someone explains a complex topic through code .

sumitmishra
Автор

Love your videos and I have subscribed to the channel.

SamuelMuto