6 package com.vesoft.nebula.client.graph.net;
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;
21 private static final long serialVersionUID = 7519424097351713021L;
25 private final CopyOnWriteArrayList<SessionWrapper> sessionList;
26 private BitSet canUseBitSet;
27 private Boolean isClose =
false;
28 private Boolean isInited =
false;
32 this.sessionList =
new CopyOnWriteArrayList<>();
36 private void checkConfig() {
37 if (config.getAddresses().isEmpty()) {
38 throw new RuntimeException(
"Empty graph addresses");
41 if (config.getSpaceName().isEmpty()) {
42 throw new RuntimeException(
"Empty space name");
59 if (canUseBitSet.isEmpty()
60 && sessionList.size() >= config.getPoolConfig().getMaxConnSize()) {
61 throw new RuntimeException(
"The SessionsManager does not have available sessions.");
63 if (!canUseBitSet.isEmpty()) {
64 int index = canUseBitSet.nextSetBit(0);
66 if (canUseBitSet.get(index)) {
67 canUseBitSet.set(index,
false);
68 return sessionList.get(index);
75 config.getUserName(), config.getPassword(), config.getReconnect());
78 throw new RuntimeException(
80 + config.getSpaceName()
85 sessionList.add(sessionWrapper);
86 return sessionWrapper;
88 throw new RuntimeException(
"Get session failed: " + e.getMessage());
100 if (session ==
null) {
103 int index = sessionList.indexOf(session);
105 Session ses = session.getSession();
107 session.setNoAvailable();
108 canUseBitSet.set(index,
true);
124 private void init() throws RuntimeException {
127 if (!pool.
init(config.getAddresses(), config.getPoolConfig())) {
128 throw new RuntimeException(
"Init pool failed: services are broken.");
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());
138 private void checkClose() {
140 throw new RuntimeException(
"The SessionsManager was closed.");
String getErrorMessage()
get the error message of the execute result
boolean isSucceeded()
the execute result is succeeded
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)
The Session is an object that operates with nebula-graph.
synchronized ResultSet execute(String stmt)
Execute the nGql sentence.
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...