diff --git a/mongo-plus-core/src/main/java/com/anwen/mongo/execute/AbstractExecute.java b/mongo-plus-core/src/main/java/com/anwen/mongo/execute/AbstractExecute.java index 89767cb8331ac945ba259bd9fa35987acaed0e7c..c016d6d271fe9ced5e4f4c88191e915feee5d611 100644 --- a/mongo-plus-core/src/main/java/com/anwen/mongo/execute/AbstractExecute.java +++ b/mongo-plus-core/src/main/java/com/anwen/mongo/execute/AbstractExecute.java @@ -55,13 +55,6 @@ public abstract class AbstractExecute implements Execute { private final Logger logger = LoggerFactory.getLogger(AbstractExecute.class); - /** - * 计数id - * @author JiaChaoYang - * @date 2023/12/28 11:33 - */ - private int num = 1; - private final CollectionManager collectionManager; private final CollectionNameConvert collectionNameConvert; @@ -416,21 +409,27 @@ public abstract class AbstractExecute implements Execute { return tableFieldMap; } - protected synchronized Integer getAutoId(Class clazz) { - MongoCollection collection = collectionManager.getCollection("counters"); - Document query = new Document(SqlOperationConstant._ID, collectionNameConvert.convert(clazz)); - Document update = new Document("$inc", new Document(SqlOperationConstant.AUTO_NUM, 1)); - Document document = Optional.ofNullable(MongoTransactionContext.getClientSessionContext()).map(session -> collection.findOneAndUpdate(session,query,update,new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER))).orElseGet(() -> collection.findOneAndUpdate(query,update,new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER))); - if (document == null){ - Integer finalNum = num; - collection.insertOne(new Document(new HashMap(){{ - put(SqlOperationConstant._ID, collectionNameConvert.convert(clazz)); - put(SqlOperationConstant.AUTO_NUM, finalNum); - }})); - }else { - num = Integer.parseInt(String.valueOf(document.get(SqlOperationConstant.AUTO_NUM))); + protected Integer getAutoId(Class clazz) { + String collectionName = collectionNameConvert.convert(clazz); + // 每个Collection单独加锁 + synchronized (collectionName.intern()) { + MongoCollection collection = collectionManager.getCollection("counters"); + Document query = new Document(SqlOperationConstant._ID, collectionName); + Document update = new Document("$inc", new Document(SqlOperationConstant.AUTO_NUM, 1)); + Document document = Optional.ofNullable(MongoTransactionContext.getClientSessionContext()) + .map(session -> collection.findOneAndUpdate(session, query, update, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER))) + .orElseGet(() -> collection.findOneAndUpdate(query, update, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER))); + int finalNum = 1; + if (document == null) { + Map map = new HashMap<>(); + map.put(SqlOperationConstant._ID, collectionNameConvert.convert(clazz)); + map.put(SqlOperationConstant.AUTO_NUM, finalNum); + collection.insertOne(new Document(map)); + } else { + finalNum = Integer.parseInt(String.valueOf(document.get(SqlOperationConstant.AUTO_NUM))); + } + return finalNum; } - return num; } protected void fillId(T entity, Document document) {