=== modified file 'src/com/mysql/jdbc/jdbc2/optional/MysqlXAConnection.java' --- src/com/mysql/jdbc/jdbc2/optional/MysqlXAConnection.java 2013-09-18 10:45:06 +0000 +++ src/com/mysql/jdbc/jdbc2/optional/MysqlXAConnection.java 2014-02-07 11:53:10 +0000 @@ -379,9 +379,9 @@ * XAER_PROTO. */ public int prepare(Xid xid) throws XAException { - StringBuffer commandBuf = new StringBuffer(); + StringBuilder commandBuf = new StringBuilder(210); commandBuf.append("XA PREPARE "); - commandBuf.append(xidToString(xid)); + appendXid(xid, commandBuf); dispatchCommand(commandBuf.toString()); @@ -421,9 +421,9 @@ * has released all held resources. */ public void rollback(Xid xid) throws XAException { - StringBuffer commandBuf = new StringBuffer(); + StringBuilder commandBuf = new StringBuilder(211); commandBuf.append("XA ROLLBACK "); - commandBuf.append(xidToString(xid)); + appendXid(xid, commandBuf); try { dispatchCommand(commandBuf.toString()); @@ -460,9 +460,9 @@ * or XA_RB*. */ public void end(Xid xid, int flags) throws XAException { - StringBuffer commandBuf = new StringBuffer(); + StringBuilder commandBuf = new StringBuilder(206); commandBuf.append("XA END "); - commandBuf.append(xidToString(xid)); + appendXid(xid, commandBuf); switch (flags) { case TMSUCCESS: @@ -503,9 +503,9 @@ * XAER_INVAL, or XAER_PROTO. */ public void start(Xid xid, int flags) throws XAException { - StringBuffer commandBuf = new StringBuffer(); + StringBuilder commandBuf = new StringBuilder(208); commandBuf.append("XA START "); - commandBuf.append(xidToString(xid)); + appendXid(xid, commandBuf); switch (flags) { case TMJOIN: @@ -548,16 +548,16 @@ */ public void commit(Xid xid, boolean onePhase) throws XAException { - StringBuffer commandBuf = new StringBuffer(); + StringBuilder commandBuf = new StringBuilder(219); commandBuf.append("XA COMMIT "); - commandBuf.append(xidToString(xid)); + appendXid(xid, commandBuf); if (onePhase) { commandBuf.append(" ONE PHASE"); } try { - dispatchCommand(commandBuf.toString()); + dispatchCommand(commandBuf.toString()); } finally { this.underlyingConnection.setInGlobalTx(false); } @@ -605,7 +605,7 @@ return new MysqlXAException(sqlEx.getMessage(), null); } - private static String xidToString(Xid xid) { + private static void appendXid(Xid xid, StringBuilder toAppendTo) { byte[] gtrid = xid.getGlobalTransactionId(); byte[] btrid = xid.getBranchQualifier(); @@ -620,48 +620,60 @@ lengthAsString += (2 * btrid.length); } - String formatIdInHex = Integer.toHexString(xid.getFormatId()); - - lengthAsString += formatIdInHex.length(); + lengthAsString += 8; // for formatId lengthAsString += 3; // for the '.' after formatId - StringBuffer asString = new StringBuffer(lengthAsString); + toAppendTo.ensureCapacity(lengthAsString + toAppendTo.length()); - asString.append("0x"); + toAppendTo.append("0x"); if (gtrid != null) { - for (int i = 0; i < gtrid.length; i++) { - String asHex = Integer.toHexString(gtrid[i] & 0xff); - - if (asHex.length() == 1) { - asString.append("0"); - } - - asString.append(asHex); - } + encode(toAppendTo, gtrid); } - asString.append(","); + toAppendTo.append(','); if (btrid != null) { - asString.append("0x"); - - for (int i = 0; i < btrid.length; i++) { - String asHex = Integer.toHexString(btrid[i] & 0xff); - - if (asHex.length() == 1) { - asString.append("0"); - } - - asString.append(asHex); - } + toAppendTo.append("0x"); + encode(toAppendTo, btrid); } - asString.append(",0x"); - asString.append(formatIdInHex); - - return asString.toString(); - } + toAppendTo.append(",0x"); + + int formatId = xid.getFormatId(); + + byte int3 = (byte) (formatId >> 24); + byte int2 = (byte) (formatId >> 16); + byte int1 = (byte) (formatId >> 8); + byte int0 = (byte) (formatId ); + if (int3 != 0) { + toAppendTo.append(DIGITS[(0xF0 & int3) >>> 4]) + .append(DIGITS[0x0F & int3]); + } + if (int2 != 0) { + toAppendTo.append(DIGITS[(0xF0 & int2) >>> 4]) + .append(DIGITS[0x0F & int2]); + } + if (int1 != 0) { + toAppendTo.append(DIGITS[(0xF0 & int1) >>> 4]) + .append(DIGITS[0x0F & int1]); + } + toAppendTo.append(DIGITS[(0xF0 & int0) >>> 4]) + .append(DIGITS[0x0F & int0]); + } + + private static final char[] DIGITS = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + + private static void encode(StringBuilder builder, byte[] bytes) { + byte b; + for (int i = 0, l = bytes.length; i < l; i++) { + b = bytes[i]; + builder.append(DIGITS[(0xF0 & b) >>> 4]) + .append(DIGITS[0x0F & b]); + } + } /* * (non-Javadoc)