Skip to content

Commit

Permalink
add config for get session from sessioPool & remove lock for session …
Browse files Browse the repository at this point in the history
…state (#539)
  • Loading branch information
Nicole00 authored Aug 30, 2023
1 parent b703e03 commit e33b81a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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);
Expand Down

0 comments on commit e33b81a

Please sign in to comment.