Skip to content

Commit

Permalink
throw exception in case of error in connection creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Harmandeep Singh committed Jul 13, 2020
1 parent 4e19edc commit 7ed5f01
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
*/
public interface ObjectFactory<T>
{
T create(String host, int socketTimeout, int connectTimeout);
T create(String host, int socketTimeout, int connectTimeout)
throws Exception;

void destroy(T t);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ public ObjectPoolPartition(ObjectPool<T> pool, PoolConfig config,
this.log = new CustomLogger(name, host);
this.takeSemaphore = new Semaphore(config.getMaxSize(), true);
for (int i = 0; i < config.getMinSize(); i++) {
T object = objectFactory.create(host, socketTimeout, connectTimeout);
if (object != null) {
try {
T object = objectFactory.create(host, socketTimeout, connectTimeout);
objectQueue.add(new Poolable<>(object, pool, host));
totalCount++;
}
catch (Exception e) {
log.warn("Error in initializing pool, error: " + e);
}
}
}

Expand Down Expand Up @@ -113,10 +116,8 @@ private Poolable<T> tryGetObject(Poolable<T> poolable)
{
try {
T object = objectFactory.create(host, socketTimeout, connectTimeout);
if (object != null) {
poolable = new Poolable<>(object, pool, host);
totalCount++;
}
poolable = new Poolable<>(object, pool, host);
totalCount++;
log.debug(String.format("Increased pool size by %d, to new size: %d, current queue size: %d, delta: %d",
1, totalCount, objectQueue.size(), 1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public SocketChannelObjectFactory(int port)

@Override
public SocketChannel create(String host, int socketTimeout, int connectTimeout)
throws IOException
{
SocketAddress sad = new InetSocketAddress(host, this.port);
SocketChannel socket = null;
SocketChannel socket;
try {
socket = SocketChannel.open();
socket.socket().setSoTimeout(socketTimeout);
Expand All @@ -49,6 +50,7 @@ public SocketChannel create(String host, int socketTimeout, int connectTimeout)
}
catch (IOException e) {
log.warn(LDS_POOL + " : Unable to open connection to host " + host, e);
throw e;
}
return socket;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@ public SocketObjectFactory(int port)

@Override
public TSocket create(String host, int socketTimeout, int connectTimeout)
throws TTransportException
{
log.debug(BKS_POOL + " : Opening connection to host: " + host);
TSocket socket = null;
TSocket socket;
try {
socket = new TSocket(host, port, socketTimeout, connectTimeout);
socket.open();
}
catch (TTransportException e) {
socket = null;
log.warn("Unable to open connection to host " + host, e);
throw e;
}
return socket;
}
Expand Down

0 comments on commit 7ed5f01

Please sign in to comment.