diff --git a/src/main/java/org/tarantool/TarantoolClientImpl.java b/src/main/java/org/tarantool/TarantoolClientImpl.java index e2567ae5..c550f991 100644 --- a/src/main/java/org/tarantool/TarantoolClientImpl.java +++ b/src/main/java/org/tarantool/TarantoolClientImpl.java @@ -19,7 +19,6 @@ import java.util.concurrent.locks.LockSupport; import java.util.concurrent.locks.ReentrantLock; - public class TarantoolClientImpl extends TarantoolBase>> implements TarantoolClient { public static final CommunicationException NOT_INIT_EXCEPTION = new CommunicationException("Not connected, initializing connection"); protected TarantoolClientConfig config; @@ -116,8 +115,8 @@ protected void reconnect(int retry, Throwable lastError) { } protected void connect(final SocketChannel channel) throws Exception { + DataInputStream is = new DataInputStream(cis = new ByteBufferInputStream(channel)); try { - DataInputStream is = new DataInputStream(cis = new ByteBufferInputStream(channel)); byte[] bytes = new byte[64]; is.readFully(bytes); String firstLine = new String(bytes); @@ -132,20 +131,22 @@ protected void connect(final SocketChannel channel) throws Exception { readPacket(is); Long code = (Long) headers.get(Key.CODE.getId()); if (code != 0) { + is.close(); throw serverError(code, body.get(Key.ERROR.getId())); } } this.is = is; } catch (IOException e) { try { - is.close(); + if (null != is) + is.close(); } catch (IOException ignored) { } try { - cis.close(); + if (null != cis) + cis.close(); } catch (IOException ignored) { - } throw new CommunicationException("Couldn't connect to tarantool", e); } @@ -426,9 +427,11 @@ protected void writeFully(SocketChannel channel, ByteBuffer buffer) throws IOExc @Override public void close() { + if (connector != null) { connector.interrupt(); } + stopIO(); }