| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.jdbcx; |
| 7 |
|
| 8 |
import java.util.Base64; |
| 9 |
import javax.transaction.xa.Xid; |
| 10 |
|
| 11 |
import org.h2.api.ErrorCode; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.message.TraceObject; |
| 14 |
|
| 15 |
/** |
| 16 |
* An object of this class represents a transaction id. |
| 17 |
*/ |
| 18 |
public final class JdbcXid extends TraceObject implements Xid { |
| 19 |
|
| 20 |
private static final String PREFIX = "XID"; |
| 21 |
|
| 22 |
private static final Base64.Encoder ENCODER = Base64.getUrlEncoder().withoutPadding(); |
| 23 |
|
| 24 |
private final int formatId; |
| 25 |
private final byte[] branchQualifier; |
| 26 |
private final byte[] globalTransactionId; |
| 27 |
|
| 28 |
JdbcXid(JdbcDataSourceFactory factory, int id, String tid) { |
| 29 |
setTrace(factory.getTrace(), TraceObject.XID, id); |
| 30 |
try { |
| 31 |
String[] splits = tid.split("\\|"); |
| 32 |
if (splits.length == 4 && PREFIX.equals(splits[0])) { |
| 33 |
formatId = Integer.parseInt(splits[1]); |
| 34 |
Base64.Decoder decoder = Base64.getUrlDecoder(); |
| 35 |
branchQualifier = decoder.decode(splits[2]); |
| 36 |
globalTransactionId = decoder.decode(splits[3]); |
| 37 |
return; |
| 38 |
} |
| 39 |
} catch (IllegalArgumentException e) { |
| 40 |
} |
| 41 |
throw DbException.get(ErrorCode.WRONG_XID_FORMAT_1, tid); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* INTERNAL |
| 46 |
* @param builder to put result into |
| 47 |
* @param xid to provide string representation for |
| 48 |
* @return provided StringBuilder |
| 49 |
*/ |
| 50 |
static StringBuilder toString(StringBuilder builder, Xid xid) { |
| 51 |
return builder.append(PREFIX).append('|').append(xid.getFormatId()) // |
| 52 |
.append('|').append(ENCODER.encodeToString(xid.getBranchQualifier())) // |
| 53 |
.append('|').append(ENCODER.encodeToString(xid.getGlobalTransactionId())); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get the format id. |
| 58 |
* |
| 59 |
* @return the format id |
| 60 |
*/ |
| 61 |
@Override |
| 62 |
public int getFormatId() { |
| 63 |
debugCodeCall("getFormatId"); |
| 64 |
return formatId; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* The transaction branch identifier. |
| 69 |
* |
| 70 |
* @return the identifier |
| 71 |
*/ |
| 72 |
@Override |
| 73 |
public byte[] getBranchQualifier() { |
| 74 |
debugCodeCall("getBranchQualifier"); |
| 75 |
return branchQualifier; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* The global transaction identifier. |
| 80 |
* |
| 81 |
* @return the transaction id |
| 82 |
*/ |
| 83 |
@Override |
| 84 |
public byte[] getGlobalTransactionId() { |
| 85 |
debugCodeCall("getGlobalTransactionId"); |
| 86 |
return globalTransactionId; |
| 87 |
} |
| 88 |
|
| 89 |
} |