use of com.alibaba.cobar.net.mysql.CommandPacket in project cobar by alibaba.
the class MySQLDetector method heartbeat.
public void heartbeat() {
if (isAuthenticated) {
String sql = heartbeat.getSource().getNode().getHeartbeatSQL();
if (sql != null) {
CommandPacket packet = new CommandPacket();
packet.packetId = 0;
packet.command = MySQLPacket.COM_QUERY;
packet.arg = sql.getBytes();
packet.write(this);
}
} else {
authenticate();
}
}
use of com.alibaba.cobar.net.mysql.CommandPacket in project cobar by alibaba.
the class MySQLChannel method sendSqlMode.
/**
* 发送SQL_MODE设置
*/
private void sendSqlMode() throws IOException {
CommandPacket cmd = getSqlModeCommand();
cmd.write(out);
out.flush();
BinaryPacket bin = receive();
switch(bin.data[0]) {
case OkPacket.FIELD_COUNT:
break;
case ErrorPacket.FIELD_COUNT:
ErrorPacket err = new ErrorPacket();
err.read(bin);
throw new ErrorPacketException(new String(err.message, charset));
default:
throw new UnknownPacketException(bin.toString());
}
}
use of com.alibaba.cobar.net.mysql.CommandPacket in project cobar by alibaba.
the class MySQLChannel method execute.
public BinaryPacket execute(RouteResultsetNode rrn, ServerConnection sc, boolean autocommit) throws IOException {
// 状态一致性检查
if (this.charsetIndex != sc.getCharsetIndex()) {
/*如果后端MySQL服务器配置了character_set_server=utf8mb4,
并且数据库客户端要求使用uft8,
我们将不发送客户端的字符集要求。
这种情况在我们使用mysql官方的Connector/J编程的时候遇见,
详细见:http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-troubleshooting.html#qandaitem-15-1-15
https://dev.mysql.com/doc/relnotes/connector-j/en/news-5-1-13.html
*/
if (this.charsetIndex != 45 && sc.getCharsetIndex() != 33)
sendCharset(sc.getCharsetIndex());
}
if (this.txIsolation != sc.getTxIsolation()) {
sendTxIsolation(sc.getTxIsolation());
}
if (this.autocommit != autocommit) {
sendAutocommit(autocommit);
}
// 生成执行数据包
CommandPacket packet = new CommandPacket();
packet.packetId = 0;
packet.command = MySQLPacket.COM_QUERY;
packet.arg = rrn.getStatement().getBytes(charset);
// 记录执行开始时间
lastActiveTime = TimeUtil.currentTimeMillis();
// 递交执行数据包并等待执行返回
packet.write(out);
out.flush();
BinaryPacket bin = receive();
// SQL执行时间统计
long now = TimeUtil.currentTimeMillis();
if (now > lastActiveTime) {
recordSql(sc.getHost(), sc.getSchema(), rrn.getStatement());
}
// 记录执行结束时间
lastActiveTime = now;
return bin;
}
use of com.alibaba.cobar.net.mysql.CommandPacket in project cobar by alibaba.
the class MySQLChannel method sendTxIsolation.
/**
* 发送事务级别设置
*/
private void sendTxIsolation(int txIsolation) throws IOException {
CommandPacket cmd = getTxIsolationCommand(txIsolation);
cmd.write(out);
out.flush();
BinaryPacket bin = receive();
switch(bin.data[0]) {
case OkPacket.FIELD_COUNT:
this.txIsolation = txIsolation;
break;
case ErrorPacket.FIELD_COUNT:
ErrorPacket err = new ErrorPacket();
err.read(bin);
throw new ErrorPacketException(new String(err.message, charset));
default:
throw new UnknownPacketException(bin.toString());
}
}
use of com.alibaba.cobar.net.mysql.CommandPacket in project cobar by alibaba.
the class MySQLChannel method killChannel.
private void killChannel() {
MySQLChannel killChannel = null;
try {
killChannel = (MySQLChannel) dataSource.getChannel();
} catch (Exception e) {
LOGGER.error("killProcess failure for getting channel", e);
return;
}
CommandPacket killPacket = new CommandPacket();
killPacket.packetId = 0;
killPacket.command = MySQLPacket.COM_QUERY;
killPacket.arg = new StringBuilder("KILL ").append(threadId).toString().getBytes();
try {
killPacket.write(killChannel.out);
killChannel.out.flush();
BinaryPacket bin = new BinaryPacket();
bin.read(killChannel.in);
switch(bin.data[0]) {
case OkPacket.FIELD_COUNT:
killChannel.release();
break;
case ErrorPacket.FIELD_COUNT:
LOGGER.error("kill error! id:" + threadId + ", err=" + bin);
killChannel.release();
break;
default:
LOGGER.error("kill unknown response, id:" + threadId + "packet=" + bin);
killChannel.close();
}
} catch (IOException e) {
killChannel.close();
LOGGER.error("kill IOException, id:" + threadId, e);
}
}
Aggregations