diff --git a/source/src/main/java/io/mycat/mycat2/MySQLSession.java b/source/src/main/java/io/mycat/mycat2/MySQLSession.java index 0ab24d5..9064032 100644 --- a/source/src/main/java/io/mycat/mycat2/MySQLSession.java +++ b/source/src/main/java/io/mycat/mycat2/MySQLSession.java @@ -9,6 +9,7 @@ import io.mycat.mycat2.beans.MySQLMetaBean; import io.mycat.mycat2.cmds.pkgread.PkgFirstReader; import io.mycat.mycat2.cmds.pkgread.PkgProcess; +import io.mycat.mycat2.console.SessionKeyEnum; import io.mycat.proxy.BufferPool; /** @@ -53,7 +54,12 @@ public void bind2MycatSession(MycatSession mycatSession) { public void unbindMycatSession() { this.useSharedBuffer(null); this.setCurBufOwner(true); //设置后端连接 获取buffer 控制权 + if(this.mycatSession != null) { + this.mycatSession.clearBeckend(this); + } this.mycatSession = null; + + this.getSessionAttrMap().remove(SessionKeyEnum.SESSION_KEY_CONN_IDLE_FLAG.getKey()); } @Override @@ -90,7 +96,7 @@ public void setMySQLMetaBean(MySQLMetaBean metaBean) { @Override public String toString() { - return "MySQLSession [database=" + database + ", ip=" + mysqlMetaBean.getDsMetaBean().getIp() + ",port=" + mysqlMetaBean.getDsMetaBean().getPort() + "]"; + return "MySQLSession [database=" + database + ", ip=" + mysqlMetaBean.getDsMetaBean().getIp() + ",port="+ mysqlMetaBean.getDsMetaBean().getPort()+ ",hashCode="+hashCode()+"]"; } } diff --git a/source/src/main/java/io/mycat/mycat2/MycatSession.java b/source/src/main/java/io/mycat/mycat2/MycatSession.java index 8af2ff8..fa51694 100644 --- a/source/src/main/java/io/mycat/mycat2/MycatSession.java +++ b/source/src/main/java/io/mycat/mycat2/MycatSession.java @@ -202,6 +202,7 @@ public void bindBackend(MySQLSession backend) { backend.setMycatSession(this); backend.useSharedBuffer(this.proxyBuffer); backend.setCurNIOHandler(this.getCurNIOHandler()); + backend.getSessionAttrMap().put(SessionKeyEnum.SESSION_KEY_CONN_IDLE_FLAG.getKey(), false); } /** @@ -212,6 +213,11 @@ public void unbindAllBackend() { backendMap.forEach((key, value) -> { if (value != null) { value.forEach(mySQLSession -> { + /*需要将前端的mycatSession设置为空 不然还会被使用*/ + MycatSession mycatSession = mySQLSession.getMycatSession(); + if(null != mycatSession) { + mycatSession.curBackend = null; + } mySQLSession.unbindMycatSession(); reactor.addMySQLSession(mySQLSession.getMySQLMetaBean(), mySQLSession); }); @@ -226,11 +232,14 @@ public void unbindBeckend(MySQLSession mysqlSession){ mysqlSession.unbindMycatSession(); list.remove(mysqlSession); } + clearBeckend(mysqlSession); + } + + public void clearBeckend(MySQLSession mysqlSession){ if(curBackend!=null&&curBackend.equals(mysqlSession)){ curBackend = null; } } - /** * 解除绑定当前 metaBean 所有的后端连接 * @param mySQLMetaBean diff --git a/source/src/main/java/io/mycat/proxy/ProxyBuffer.java b/source/src/main/java/io/mycat/proxy/ProxyBuffer.java index 3c6391d..e43fc6c 100644 --- a/source/src/main/java/io/mycat/proxy/ProxyBuffer.java +++ b/source/src/main/java/io/mycat/proxy/ProxyBuffer.java @@ -1,6 +1,5 @@ package io.mycat.proxy; -import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import org.slf4j.Logger; @@ -414,7 +413,7 @@ public byte[] getLenencBytes(int index) { * 值 * @return 长度 */ - public int getLenencLength(int lenenc) { + public static int getLenencLength(int lenenc) { if (lenenc < 251) { return 1; } else if (lenenc >= 251 && lenenc < (1 << 16)) { @@ -426,6 +425,7 @@ public int getLenencLength(int lenenc) { } } + public long getLenencInt(int index) { long len = getInt(index, 1) & 0xff; if (len == 0xfc) {