Skip to content

Commit

Permalink
----1. 修正, pgsql native下 实物回滚问题, 2.更新插入语句未返回更改行数问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fireflyhoo committed Nov 24, 2016
1 parent 816679f commit c02d883
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public void rollback() {
conn.setResponseHandler(RollbackNodeHandler.this);

//support the XA rollback
MySQLConnection mysqlCon = (MySQLConnection) conn;
if(session.getXaTXID()!=null) {
if(session.getXaTXID()!=null && conn instanceof MySQLConnection) {
MySQLConnection mysqlCon = (MySQLConnection) conn;
String xaTxId = session.getXaTXID();
//exeBatch cmd issue : the 2nd package can not receive the response
mysqlCon.execCmd("XA END " + xaTxId + ";");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,11 @@ private void doProcessCommandComplete(PostgreSQLBackendConnection con,
doProcessBusinessQuery(con, response, commandComplete);
} else {
OkPacket okPck = new OkPacket();
okPck.affectedRows = 0;
okPck.insertId = 0;

okPck.affectedRows =commandComplete.getAffectedRows();
okPck.insertId =commandComplete.getInsertId();
okPck.packetId = ++packetId;
okPck.message = commandComplete.getCommandResponse().trim()
.getBytes();
okPck.message = commandComplete.getCommandResponse().getBytes();
con.getResponseHandler().okResponse(okPck.writeToBytes(), con);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// String
// 命令标记。它通常是一个单字,标识那个命令完成。
//
// 对于INSERT命令,标记是INSERT oidrows, 这里的rows是插入的行数。oid 在row为 1 并且目标表有 OID 的时候是插入行的对象 ID; 否则oid就是 0。
// 对于INSERT命令,标记是INSERT oid rows, 这里的rows是插入的行数。oid 在row为 1 并且目标表有 OID 的时候是插入行的对象 ID; 否则oid就是 0。
//
// 对于DELETE 命令,标记是 DELETE rows, 这里的 rows 是删除的行数。
//
Expand All @@ -32,6 +32,30 @@ public class CommandComplete extends PostgreSQLPacket {
*/
private String commandResponse;

// 存储状态。

public int getAffectedRows() {
return affectedRows;
}

public void setAffectedRows(int affectedRows) {
this.affectedRows = affectedRows;
}

public int getInsertId() {
return insertId;
}

public void setInsertId(int insertId) {
this.insertId = insertId;
}

// 修改影响条数
private int affectedRows = 0;

// 插入ID
private int insertId = 0;

@Override
public int getLength() {
return length;
Expand All @@ -41,17 +65,18 @@ public boolean isDDLComplete() {
return commandResponse != null && (commandResponse.startsWith("INSERT") || commandResponse.startsWith("DELETE")
|| commandResponse.startsWith("UPDATE"));
}

public boolean isTranComplete(){
return commandResponse != null && (commandResponse.startsWith("ROLLBACK") || commandResponse.startsWith("COMMIT"));

public boolean isTranComplete() {
return commandResponse != null
&& (commandResponse.startsWith("ROLLBACK") || commandResponse.startsWith("COMMIT"));
}

public boolean isSelectComplete() {
return commandResponse != null && (commandResponse.startsWith("SELECT"));
}

public int getRows() {
if(!isDDLComplete()){
if (!isDDLComplete()) {
return 0;
}
if (commandResponse != null) {
Expand Down Expand Up @@ -79,14 +104,35 @@ public static CommandComplete parse(ByteBuffer buffer, int offset) {
}
CommandComplete packet = new CommandComplete();
packet.length = PIOUtils.redInteger4(buffer, offset + 1);
packet.commandResponse = new String(PIOUtils.redByteArray(buffer, offset + 1 + 4, packet.length - 4), UTF8);
packet.commandResponse = new String(PIOUtils.redByteArray(buffer, offset + 1 + 4, packet.length - 4), UTF8)
.trim();
if (packet.commandResponse.startsWith("INSERT")) {
String vs[] = packet.commandResponse.replace("INSERT", "").trim().split(" +");
packet.insertId = parseInt(vs[0]);

packet.affectedRows =parseInt(vs[1]);
} else if (packet.commandResponse.startsWith("UPDATE")) {
packet.affectedRows = parseInt(packet.commandResponse.replace("UPDATE", "").trim());
}else if(packet.commandResponse.startsWith("DELETE")){
packet.affectedRows = parseInt(packet.commandResponse.replace("DELETE", "").trim());
}
return packet;

}



private static int parseInt(String value) {
try{
return Integer.parseInt(value);
}catch (Exception e) {
e.printStackTrace();
}
return 0;
}

public String getCommandResponse() {
return commandResponse;
}


}

0 comments on commit c02d883

Please sign in to comment.