From 2f6c8b46365c2f0665369eb5c5f4cd6a4cb3bba7 Mon Sep 17 00:00:00 2001 From: wangxiaoyan Date: Wed, 31 Jan 2024 14:39:20 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=96=B0=E7=9A=84Collect?= =?UTF-8?q?ion=E6=8F=92=E5=85=A5=E6=97=B6=EF=BC=8C=E8=87=AA=E5=A2=9Eid?= =?UTF-8?q?=E4=BC=9A=E5=8F=96=E4=B8=8A=E4=B8=AACollection=E7=9A=84?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E5=80=BC=E3=80=81=E8=87=AA=E5=A2=9Eid?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E9=94=81=E7=B2=92=E5=BA=A6=E7=BC=A9=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/anwen/mongo/execute/SqlExecute.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/mongo-plus-core/src/main/java/com/anwen/mongo/execute/SqlExecute.java b/mongo-plus-core/src/main/java/com/anwen/mongo/execute/SqlExecute.java index 91c6155..cf041b4 100644 --- a/mongo-plus-core/src/main/java/com/anwen/mongo/execute/SqlExecute.java +++ b/mongo-plus-core/src/main/java/com/anwen/mongo/execute/SqlExecute.java @@ -104,9 +104,6 @@ public class SqlExecute { private String createIndex = null; - private int num = 1; - - public void init(Class clazz) { String tableName = clazz.getSimpleName().toLowerCase(); String database = baseProperty.getDatabase(); @@ -1088,20 +1085,26 @@ public class SqlExecute { } private synchronized Integer getAutoId(Class clazz) { + String collectionName = collectionNameConvert.convert(clazz); MongoCollection collection = getCollection("counters"); - Document query = new Document(SqlOperationConstant._ID, collectionNameConvert.convert(clazz)); + 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))); - 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))); + // 只锁当前collection + synchronized (collectionName.intern()) { + 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; } private void fillId(T entity, Document document) { -- Gitee From 50cdaa9db5aab6148680b4066676f0006bd5558a Mon Sep 17 00:00:00 2001 From: wangxiaoyan Date: Wed, 31 Jan 2024 15:23:10 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=B0Collection?= =?UTF-8?q?=E8=87=AA=E5=A2=9Eid=E4=BC=9A=E4=BB=8E=E4=B8=8A=E4=B8=AACollect?= =?UTF-8?q?ion=E7=9A=84id=E5=BC=80=E5=A7=8B=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../anwen/mongo/execute/AbstractExecute.java | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) 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 89767cb..c256249 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, collection); + 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) { -- Gitee From 4ea0b143d8946eb834fdece5fca589b5a6542012 Mon Sep 17 00:00:00 2001 From: wangxiaoyan Date: Wed, 31 Jan 2024 15:33:55 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=B0Collection?= =?UTF-8?q?=E8=87=AA=E5=A2=9Eid=E4=BC=9A=E4=BB=8E=E4=B8=8A=E4=B8=AACollect?= =?UTF-8?q?ion=E7=9A=84id=E5=BC=80=E5=A7=8B=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/anwen/mongo/execute/AbstractExecute.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c256249..c016d6d 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 @@ -414,7 +414,7 @@ public abstract class AbstractExecute implements Execute { // 每个Collection单独加锁 synchronized (collectionName.intern()) { MongoCollection collection = collectionManager.getCollection("counters"); - Document query = new Document(SqlOperationConstant._ID, collection); + 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))) -- Gitee