Connection Pooling in Java Client

I had write some Java code .
I don’t know whether it helps.

DGraphClientFactory.java

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;


public class DGraphClientFactory extends BasePooledObjectFactory<DgraphClient> {

  private String host;
  private Integer port;

  DGraphClientFactory(String host, Integer port) {
    this.host = host;
    this.port = port;
  }

  @Override
  public DgraphClient create() throws Exception {
    ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext(true)
        .build();
    DgraphBlockingStub blockingStub = DgraphGrpc.newBlockingStub(channel);
    return new DgraphClient(Collections.singletonList(blockingStub));
  }

  @Override
  public PooledObject<DgraphClient> wrap(DgraphClient dgraphClient) {
    return new DefaultPooledObject<DgraphClient>(dgraphClient);
  }
}

DefaultPooledDGraphClient.java


public class DefaultPooledDGraphClient implements
    PooledDGraphClient {

  private static Logger logger = LoggerFactory
      .getLogger(DefaultPooledDGraphClient.class);
  private final SoftReferenceObjectPool<DgraphClient> pool;

  public DefaultPooledDGraphClient(GraphConfiguration configuration) {
    String name = configuration.getString("dgraph.host", "localhost");
    Integer port = configuration.getInteger("dgraph.port", 9080);
    logger.info("PooledGraphConfig:", configuration);
    pool = new SoftReferenceObjectPool(new DGraphClientFactory(name, port));
  }

  @Override
  public DgraphClient getClient() {
    try {
      return pool.borrowObject();
    } catch (Exception e) {
      throw new RuntimeException("Unable to borrow DGraphClient from pool" +
          e.toString());
    }
  }

  public void releaseClient(DgraphClient client) {
    try {
      pool.returnObject(client);
    } catch (Exception e) {
      throw new RuntimeException("Unable to release DGraphClient " + e.toString());
    }
  }
}
<dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
      <version>2.5.0</version>
    </dependency>

:joy: I pooled the client. not the connection .
You can do it yourself .