com.anwen.mongo.context.MongoTransactionContext Maven / Gradle / Ivy
package com.anwen.mongo.context;
import com.mongodb.client.ClientSession;
/**
* * @description TODO
* * @author [email protected]
* * @date 2023/09/17 17:42
*/
public class MongoTransactionContext {
private static final ThreadLocal threadLocalHeaderMap = new ThreadLocal<>();
public MongoTransactionContext() {
}
public static ClientSession getClientSessionContext() {
MongoTransactionStatus status = getMongoTransactionStatus();
if (status != null) {
return status.getClientSession();
}
return null;
}
public static MongoTransactionStatus getMongoTransactionStatus() {
return threadLocalHeaderMap.get();
}
public static void setTransactionStatus(MongoTransactionStatus mongoTransactionStatus) {
threadLocalHeaderMap.set(mongoTransactionStatus);
}
public static void clear() {
threadLocalHeaderMap.remove();
}
}