NebulaGraph Java Client  release-3.8
NebulaSession.java
1 /* Copyright (c) 2022 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;
7 
8 import com.vesoft.nebula.Value;
9 import com.vesoft.nebula.client.graph.data.ResultSet;
10 import com.vesoft.nebula.client.graph.exception.IOErrorException;
11 import com.vesoft.nebula.client.graph.net.Session;
12 import com.vesoft.nebula.client.graph.net.SessionState;
13 import com.vesoft.nebula.client.graph.net.SyncConnection;
14 import java.io.Serializable;
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.concurrent.atomic.AtomicBoolean;
18 import java.util.concurrent.atomic.AtomicReference;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 
22 public class NebulaSession implements Serializable {
23 
24  private static final long serialVersionUID = -88438249377120255L;
25 
26  private final Logger log = LoggerFactory.getLogger(this.getClass());
27 
28  private final long sessionID;
29  private final int timezoneOffset;
30  private SyncConnection connection;
31  private final AtomicReference<SessionState> state = new AtomicReference<>();
32 
33  private final AtomicBoolean isReleased = new AtomicBoolean(false);
34 
35  public NebulaSession(SyncConnection connection, long sessionID, int timezoneOffset,
36  SessionState state) {
37  this.connection = connection;
38  this.sessionID = sessionID;
39  this.timezoneOffset = timezoneOffset;
40  this.state.set(state);
41  }
42 
43  public long getSessionID() {
44  return sessionID;
45  }
46 
47  public Boolean isIdle() {
48  return state.get() == SessionState.IDLE;
49  }
50 
51  public Boolean isUsed() {
52  return state.get() == SessionState.USED;
53  }
54 
55  public boolean isUsedAndSetIdle() {
56  return state.compareAndSet(SessionState.USED, SessionState.IDLE);
57  }
58 
59  public boolean isIdleAndSetUsed() {
60  return state.compareAndSet(SessionState.IDLE, SessionState.USED);
61  }
62 
63  public ResultSet execute(String stmt) throws IOErrorException {
64  return new ResultSet(connection.execute(sessionID, stmt), timezoneOffset);
65  }
66 
67  public ResultSet executeWithParameter(String stmt, Map<String, Object> parameterMap)
68  throws IOErrorException {
69  Map<byte[], Value> map = new HashMap<>();
70  parameterMap.forEach((key, value) -> map.put(key.getBytes(), Session.value2Nvalue(value)));
71  return new ResultSet(connection.executeWithParameter(sessionID, stmt, map), timezoneOffset);
72  }
73 
74  public String executeJsonWithParameter(String stmt, Map<String, Object> parameterMap)
75  throws IOErrorException {
76  Map<byte[], Value> map = new HashMap<>();
77  parameterMap.forEach((key, value) -> map.put(key.getBytes(), Session.value2Nvalue(value)));
78  return connection.executeJsonWithParameter(sessionID, stmt, map);
79  }
80 
81  public void release() {
82  if (isReleased.get()) {
83  return;
84  }
85  if (isReleased.compareAndSet(false, true)) {
86  try {
87  connection.signout(sessionID);
88  connection.close();
89  } catch (Exception e) {
90  // not print the warn to avoid confuse for session and connect,
91  // when connection is broken, release will failed, just make connection as null.
92  // log.warn("release session failed, " + e.getMessage());
93  }
94  connection = null;
95  }
96  }
97 }
The Session is an object that operates with nebula-graph.
Definition: Session.java:53
static Value value2Nvalue(Object value)
convert java value type to nebula thrift value type
Definition: Session.java:434