Skip to content

Commit

Permalink
Merge pull request #80 from zwyqz/master
Browse files Browse the repository at this point in the history
解决绑定后端连接之后 还有前端的mycatSession依旧指向旧的mysqlssion
  • Loading branch information
apachemycat authored Sep 25, 2017
2 parents 568e37c + e8cd62d commit 9dd2815
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
8 changes: 7 additions & 1 deletion source/src/main/java/io/mycat/mycat2/MySQLSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()+"]";
}

}
11 changes: 10 additions & 1 deletion source/src/main/java/io/mycat/mycat2/MycatSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
});
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions source/src/main/java/io/mycat/proxy/ProxyBuffer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.mycat.proxy;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import org.slf4j.Logger;
Expand Down Expand Up @@ -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)) {
Expand All @@ -426,6 +425,7 @@ public int getLenencLength(int lenenc) {
}
}


public long getLenencInt(int index) {
long len = getInt(index, 1) & 0xff;
if (len == 0xfc) {
Expand Down

0 comments on commit 9dd2815

Please sign in to comment.