diff --git a/client/src/main/java/com/vesoft/nebula/client/graph/SessionPool.java b/client/src/main/java/com/vesoft/nebula/client/graph/SessionPool.java index 22ae4f343..b4fc7f7d3 100644 --- a/client/src/main/java/com/vesoft/nebula/client/graph/SessionPool.java +++ b/client/src/main/java/com/vesoft/nebula/client/graph/SessionPool.java @@ -79,7 +79,7 @@ public SessionPool(SessionPoolConfig poolConfig) { */ private synchronized NebulaSession getSession() throws ClientServerIncompatibleException, AuthFailedException, IOErrorException, BindSpaceFailedException { - int retry = 1; + int retry = sessionPoolConfig.getRetryConnectTimes(); while (retry-- >= 0) { // if there are idle sessions, get session from queue if (idleSessionSize.get() > 0) { @@ -285,7 +285,7 @@ public int getIdleSessionNums() { /** * release the NebulaSession when finished the execution. */ - private synchronized void releaseSession(NebulaSession nebulaSession) { + private void releaseSession(NebulaSession nebulaSession) { nebulaSession.isUsedAndSetIdle(); idleSessionSize.incrementAndGet(); } diff --git a/client/src/main/java/com/vesoft/nebula/client/graph/SessionPoolConfig.java b/client/src/main/java/com/vesoft/nebula/client/graph/SessionPoolConfig.java index 10346fa7f..898739c31 100644 --- a/client/src/main/java/com/vesoft/nebula/client/graph/SessionPoolConfig.java +++ b/client/src/main/java/com/vesoft/nebula/client/graph/SessionPoolConfig.java @@ -35,6 +35,9 @@ public class SessionPoolConfig implements Serializable { // The healthCheckTime for schedule check the health of session, unit: second private int healthCheckTime = 600; + // retry times to get session + private int retryConnectTimes = 1; + // The wait time to get idle connection, unit ms private int waitTime = 0; @@ -147,6 +150,18 @@ public SessionPoolConfig setHealthCheckTime(int healthCheckTime) { return this; } + public int getRetryConnectTimes() { + return retryConnectTimes; + } + + public SessionPoolConfig setRetryConnectTimes(int retryConnectTimes) { + if (retryConnectTimes < 0) { + throw new IllegalArgumentException("retryConnectTimes cannot be less than 0."); + } + this.retryConnectTimes = retryConnectTimes; + return this; + } + public int getWaitTime() { return waitTime; } diff --git a/examples/src/main/java/com/vesoft/nebula/examples/GraphSessionPoolExample.java b/examples/src/main/java/com/vesoft/nebula/examples/GraphSessionPoolExample.java index 6b6518e7d..1146a3277 100644 --- a/examples/src/main/java/com/vesoft/nebula/examples/GraphSessionPoolExample.java +++ b/examples/src/main/java/com/vesoft/nebula/examples/GraphSessionPoolExample.java @@ -34,8 +34,14 @@ public static void main(String[] args) { String spaceName = "test"; String user = "root"; String password = "nebula"; - SessionPoolConfig sessionPoolConfig = new SessionPoolConfig(addresses, spaceName, user, - password); + SessionPoolConfig sessionPoolConfig = + new SessionPoolConfig(addresses, spaceName, user, password) + .setMaxSessionSize(10) + .setMinSessionSize(10) + .setRetryConnectTimes(3) + .setWaitTime(100) + .setRetryTimes(3) + .setIntervalTime(100); SessionPool sessionPool = new SessionPool(sessionPoolConfig); if (!sessionPool.init()) { log.error("session pool init failed."); @@ -46,7 +52,7 @@ public static void main(String[] args) { resultSet = sessionPool.execute("match (v:player) return v limit 1;"); System.out.println(resultSet.toString()); } catch (IOErrorException | ClientServerIncompatibleException | AuthFailedException - | BindSpaceFailedException e) { + | BindSpaceFailedException e) { e.printStackTrace(); sessionPool.close(); System.exit(1);