Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -819,16 +819,20 @@ public int currentOccupiedSize() {

@SuppressWarnings({"squid:S2446"})
protected void putBack(ISession session) {
queue.push(session);
synchronized (this) {
// we do not need to notifyAll as any waited thread can continue to work after waked up.
this.notify();
// comment the following codes as putBack is too frequently called.
// if (logger.isTraceEnabled()) {
// logger.trace("put a session back and notify others..., queue.size = {}",
// queue.size());
// }
if (!closed) {
queue.push(session);
// we do not need to notifyAll as any waited thread can continue to work after waked up.
this.notify();
// comment the following codes as putBack is too frequently called.
// if (logger.isTraceEnabled()) {
// logger.trace("put a session back and notify others..., queue.size = {}",
// queue.size());
// }
return;
}
}
closeSession(session);
}

private void occupy(ISession session) {
Expand Down Expand Up @@ -872,22 +876,41 @@ public synchronized void close() {

@Override
public void closeResultSet(SessionDataSetWrapper wrapper) {
ISession session;
synchronized (wrapper) {
session = wrapper.getSession();
if (session == null) {
return;
}
// Detach before returning the session, so closing an old wrapper cannot release a new query.
wrapper.setSession(null);
session = occupied.remove(session);
}
if (session == null) {
return;
}
boolean putback = true;
try {
wrapper.getSessionDataSet().closeOperationHandle();
} catch (IoTDBConnectionException | StatementExecutionException e) {
tryConstructNewSession();
putback = false;
cleanSessionAndMayThrowConnectionException(session);
} finally {
ISession session = occupied.remove(wrapper.getSession());
if (putback && session != null) {
putBack(wrapper.getSession());
if (putback) {
putBack(session);
}
}
}

@SuppressWarnings({"squid:S2446"})
private void tryConstructNewSession() {
synchronized (this) {
if (closed) {
size--;
this.notify();
return;
}
}
Session session = constructNewSession();
try {
session.open(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ protected TableSessionWrapper(Session session, SessionPool sessionPool) {
}

@Override
public void insert(Tablet tablet) throws StatementExecutionException, IoTDBConnectionException {
public synchronized void insert(Tablet tablet)
throws StatementExecutionException, IoTDBConnectionException {
try {
session.insertRelationalTablet(tablet);
} catch (IoTDBConnectionException e) {
Expand All @@ -69,7 +70,7 @@ public void insert(Tablet tablet) throws StatementExecutionException, IoTDBConne
}

@Override
public SessionDataSet executeQueryStatement(String sql)
public synchronized SessionDataSet executeQueryStatement(String sql)
throws StatementExecutionException, IoTDBConnectionException {
try {
return session.executeQueryStatement(sql);
Expand All @@ -82,7 +83,7 @@ public SessionDataSet executeQueryStatement(String sql)
}

@Override
public SessionDataSet executeQueryStatement(String sql, long timeoutInMs)
public synchronized SessionDataSet executeQueryStatement(String sql, long timeoutInMs)
throws StatementExecutionException, IoTDBConnectionException {
try {
return session.executeQueryStatement(sql, timeoutInMs);
Expand All @@ -95,7 +96,7 @@ public SessionDataSet executeQueryStatement(String sql, long timeoutInMs)
}

@Override
public void executeNonQueryStatement(String sql)
public synchronized void executeNonQueryStatement(String sql)
throws IoTDBConnectionException, StatementExecutionException {
try {
session.executeNonQueryStatement(sql);
Expand All @@ -108,40 +109,46 @@ public void executeNonQueryStatement(String sql)
}

@Override
public void close() throws IoTDBConnectionException {
if (!Objects.equals(session.getSqlDialect(), sessionPool.sqlDialect)) {
try {
session.executeNonQueryStatement("set sql_dialect=" + sessionPool.sqlDialect);
} catch (StatementExecutionException e) {
LOGGER.warn(
SessionMessages
.LOG_FAILED_CHANGE_BACK_SQL_DIALECT_EXECUTING_SET_SQL_DIALECT_ARG_947F35E7,
sessionPool.sqlDialect,
e);
session.close();
session = null;
return;
}
public synchronized void close() throws IoTDBConnectionException {
if (!closed.compareAndSet(false, true)) {
return;
}

if (closed.compareAndSet(false, true)) {
if (!Objects.equals(session.getDatabase(), sessionPool.database)
Session closingSession = session;
session = null;
try {
if (!Objects.equals(closingSession.getSqlDialect(), sessionPool.sqlDialect)) {
try {
closingSession.executeNonQueryStatement("set sql_dialect=" + sessionPool.sqlDialect);
} catch (StatementExecutionException e) {
LOGGER.warn(
SessionMessages
.LOG_FAILED_CHANGE_BACK_SQL_DIALECT_EXECUTING_SET_SQL_DIALECT_ARG_947F35E7,
sessionPool.sqlDialect,
e);
sessionPool.cleanSessionAndMayThrowConnectionException(closingSession);
return;
}
}

if (!Objects.equals(closingSession.getDatabase(), sessionPool.database)
&& sessionPool.database != null) {
try {
session.executeNonQueryStatement("use " + sessionPool.database);
closingSession.executeNonQueryStatement("use " + sessionPool.database);
} catch (StatementExecutionException e) {
LOGGER.warn(
SessionMessages.LOG_FAILED_CHANGE_BACK_DATABASE_EXECUTING_USE_ARG_274541CA,
sessionPool.database,
e);
session.close();
session = null;
sessionPool.cleanSessionAndMayThrowConnectionException(closingSession);
return;
}
}

sessionPool.putBack(session);
session = null;
sessionPool.putBack(closingSession);
} catch (IoTDBConnectionException e) {
sessionPool.cleanSessionAndMayThrowConnectionException(closingSession);
throw e;
}
}
}
Loading
Loading