NebulaGraph Java Client  release-3.8
SessionsManager.java
1 /* Copyright (c) 2021 vesoft inc. All rights reserved.
2  *
3  * This source code is licensed under Apache 2.0 License.
4  */
5 
6 package com.vesoft.nebula.client.graph.net;
7 
8 import com.vesoft.nebula.client.graph.SessionsManagerConfig;
9 import com.vesoft.nebula.client.graph.data.ResultSet;
10 import com.vesoft.nebula.client.graph.exception.AuthFailedException;
11 import com.vesoft.nebula.client.graph.exception.ClientServerIncompatibleException;
12 import com.vesoft.nebula.client.graph.exception.IOErrorException;
13 import com.vesoft.nebula.client.graph.exception.NotValidConnectionException;
14 import java.io.Serializable;
15 import java.net.UnknownHostException;
16 import java.util.BitSet;
17 import java.util.concurrent.CopyOnWriteArrayList;
18 
19 public class SessionsManager implements Serializable {
20 
21  private static final long serialVersionUID = 7519424097351713021L;
22 
23  private final SessionsManagerConfig config;
24  private NebulaPool pool = null;
25  private final CopyOnWriteArrayList<SessionWrapper> sessionList;
26  private BitSet canUseBitSet;
27  private Boolean isClose = false;
28  private Boolean isInited = false;
29 
30  public SessionsManager(SessionsManagerConfig config) {
31  this.config = config;
32  this.sessionList = new CopyOnWriteArrayList<>();
33  checkConfig();
34  }
35 
36  private void checkConfig() {
37  if (config.getAddresses().isEmpty()) {
38  throw new RuntimeException("Empty graph addresses");
39  }
40 
41  if (config.getSpaceName().isEmpty()) {
42  throw new RuntimeException("Empty space name");
43  }
44  }
45 
53  public synchronized SessionWrapper getSessionWrapper() throws RuntimeException,
55  checkClose();
56  if (!isInited) {
57  init();
58  }
59  if (canUseBitSet.isEmpty()
60  && sessionList.size() >= config.getPoolConfig().getMaxConnSize()) {
61  throw new RuntimeException("The SessionsManager does not have available sessions.");
62  }
63  if (!canUseBitSet.isEmpty()) {
64  int index = canUseBitSet.nextSetBit(0);
65  if (index >= 0) {
66  if (canUseBitSet.get(index)) {
67  canUseBitSet.set(index, false);
68  return sessionList.get(index);
69  }
70  }
71  }
72  // create new session
73  try {
74  Session session = pool.getSession(
75  config.getUserName(), config.getPassword(), config.getReconnect());
76  ResultSet resultSet = session.execute("USE " + config.getSpaceName());
77  if (!resultSet.isSucceeded()) {
78  throw new RuntimeException(
79  "Switch space `"
80  + config.getSpaceName()
81  + "' failed: "
82  + resultSet.getErrorMessage());
83  }
84  SessionWrapper sessionWrapper = new SessionWrapper(session);
85  sessionList.add(sessionWrapper);
86  return sessionWrapper;
88  throw new RuntimeException("Get session failed: " + e.getMessage());
89  }
90  }
91 
98  public synchronized void returnSessionWrapper(SessionWrapper session) {
99  checkClose();
100  if (session == null) {
101  return;
102  }
103  int index = sessionList.indexOf(session);
104  if (index >= 0) {
105  Session ses = session.getSession();
106  sessionList.set(index, new SessionWrapper(ses));
107  session.setNoAvailable();
108  canUseBitSet.set(index, true);
109  }
110  }
111 
115  public synchronized void close() {
116  for (SessionWrapper session : sessionList) {
117  session.release();
118  }
119  pool.close();
120  sessionList.clear();
121  isClose = true;
122  }
123 
124  private void init() throws RuntimeException {
125  try {
126  pool = new NebulaPool();
127  if (!pool.init(config.getAddresses(), config.getPoolConfig())) {
128  throw new RuntimeException("Init pool failed: services are broken.");
129  }
130  canUseBitSet = new BitSet(config.getPoolConfig().getMaxConnSize());
131  canUseBitSet.set(0, config.getPoolConfig().getMaxConnSize(), false);
132  } catch (UnknownHostException e) {
133  throw new RuntimeException("Init the pool failed: " + e.getMessage());
134  }
135  isInited = true;
136  }
137 
138  private void checkClose() {
139  if (isClose) {
140  throw new RuntimeException("The SessionsManager was closed.");
141  }
142  }
143 }
String getErrorMessage()
get the error message of the execute result
Definition: ResultSet.java:180
boolean isSucceeded()
the execute result is succeeded
Definition: ResultSet.java:144
Session getSession(String userName, String password, boolean reconnect)
get a session from the NebulaPool
void close()
close the pool, all connections will be closed
boolean init(List< HostAddress > addresses, NebulaPoolConfig config)
Definition: NebulaPool.java:78
The Session is an object that operates with nebula-graph.
Definition: Session.java:53
synchronized ResultSet execute(String stmt)
Execute the nGql sentence.
Definition: Session.java:91
synchronized void close()
close: release all sessions and close the connection pool
synchronized void returnSessionWrapper(SessionWrapper session)
returnSessionWrapper: return the SessionWrapper to the sessionManger, the old SessionWrapper couldn't...
synchronized SessionWrapper getSessionWrapper()
getSessionWrapper: return a SessionWrapper from sessionManager, the SessionWrapper couldn't use by mu...