Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:fix UUID generate blocked #507

Merged
merged 3 commits into from
Jun 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.security.SecureRandom;
import java.util.UUID;


/**
* 本地事务工具类
*
Expand All @@ -28,6 +29,35 @@
*/
@Slf4j
public final class LocalTxUtil {
private static final ThreadLocal<SecureRandom> SECURE_RANDOM_HOLDER = new ThreadLocal<SecureRandom>() {
@Override
protected SecureRandom initialValue() {
return new SecureRandom();
}
};

public static UUID randomUUID(){
SecureRandom ng = SECURE_RANDOM_HOLDER.get();
byte[] randomBytes = new byte[16];
ng.nextBytes(randomBytes);
// clear version
randomBytes[6] &= 0x0f;
// set to version 4
randomBytes[6] |= 0x40;
// clear variant
randomBytes[8] &= 0x3f;
// set to IETF variant
randomBytes[8] |= 0x80;
long msb = 0;
long lsb = 0;
for (int i=0; i<8; i++) {
msb = (msb << 8) | (randomBytes[i] & 0xff);
}
for (int i=8; i<16; i++) {
lsb = (lsb << 8) | (randomBytes[i] & 0xff);
}
return new UUID(msb, lsb);
}

/**
* 手动开启事务
Expand All @@ -37,7 +67,7 @@ public static String startTransaction() {
if (!StringUtils.isEmpty(xid)) {
log.debug("dynamic-datasource exist local tx [{}]", xid);
} else {
xid = UUID.randomUUID().toString();
xid = randomUUID().toString();
TransactionContext.bind(xid);
log.debug("dynamic-datasource start local tx [{}]", xid);
}
Expand Down