All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anwen.mongo.context.MongoTransactionSpring Maven / Gradle / Ivy

There is a newer version: 2.1.6.1
Show newest version
package com.anwen.mongo.context;

import com.mongodb.client.ClientSession;

import java.util.Map;

/**
 * 使用spring中的事务
 * @author JiaChaoYang
 **/
public class MongoTransactionSpring {

    private static final ThreadLocal> resources =
            new ThreadLocal<>();

    private static final ThreadLocal currentTransactionName =
            new ThreadLocal<>();

    public static void setResources(Map resource){
        resources.set(resource);
    }

    public static Map getResourceMap(){
        return resources.get();
    }

    public static ClientSession getResource(Object key){
        Map resourceMap = resources.get();
        return resourceMap == null ? null : (ClientSession) resourceMap.get(key);
    }

    public static ClientSession getResource(){
        return resources.get() != null && getCurrentTransactionName() != null ? getResource(currentTransactionName.get()) : null;
    }

    public static void setCurrentTransactionName(String name) {
        currentTransactionName.set(name);
    }

    public static String getCurrentTransactionName() {
        return currentTransactionName.get();
    }

    public static void clear(){
        resources.remove();
        currentTransactionName.remove();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy