diff --git a/orm/src/main/java/com/zfoo/orm/model/cache/EntityCaches.java b/orm/src/main/java/com/zfoo/orm/model/cache/EntityCaches.java index 8fbd9275..1a43d3e0 100644 --- a/orm/src/main/java/com/zfoo/orm/model/cache/EntityCaches.java +++ b/orm/src/main/java/com/zfoo/orm/model/cache/EntityCaches.java @@ -94,7 +94,6 @@ public class EntityCaches, E extends IEntity> impl // 如果数据库中不存在则给一个默认值 if (entity == null) { entity = (E) entityDef.newEntity(pk); - logger.error("数据库[{}]没有包含主键[pk:{}]的文档,返回默认值", entityDef.getClazz().getSimpleName(), pk); } return new PNode(entity); @@ -144,6 +143,11 @@ public class EntityCaches, E extends IEntity> impl cache.put(entity.id(), currentPnode); } + if (currentPnode.getThreadId() != Thread.currentThread().getId()) { + logger.error("[{}]被多线程访问了,先后2次访问线程分别是[{}] [{}]", entity.getClass().getSimpleName(), + currentPnode.getThreadName(), Thread.currentThread().getName()); + } + // 加100以防止,立刻加载并且立刻修改数据的情况发生时,服务器取到的时间戳相同 currentPnode.setModifiedTime(TimeUtils.now() + 100); } @@ -241,7 +245,7 @@ public class EntityCaches, E extends IEntity> impl var ids = updateList.stream().map(it -> it.id()).collect(Collectors.toList()); try { - var dbList = OrmContext.getQuery((Class)entityDef.getClazz()).in("_id", ids).queryAll(); + var dbList = OrmContext.getQuery((Class) entityDef.getClazz()).in("_id", ids).queryAll(); var dbMap = dbList.stream().collect(Collectors.toMap(key -> key.id(), value -> value)); for (var entity : updateList) { var dbEntity = dbMap.get(entity.id()); diff --git a/orm/src/main/java/com/zfoo/orm/model/entity/IEntity.java b/orm/src/main/java/com/zfoo/orm/model/entity/IEntity.java index 454b08e7..d6739b8b 100644 --- a/orm/src/main/java/com/zfoo/orm/model/entity/IEntity.java +++ b/orm/src/main/java/com/zfoo/orm/model/entity/IEntity.java @@ -13,6 +13,8 @@ package com.zfoo.orm.model.entity; +import com.zfoo.protocol.util.StringUtils; + /** * @author jaysunxiao * @version 3.0 @@ -46,4 +48,25 @@ public interface IEntity> { default void svs(long vs) { } + /** + * 由于查询不存在时缓存中也会有一份,因此判断为空需要根据实际类型才能决定 + * + * @return + */ + default boolean isNull() { + PK idValue = id(); + if (idValue == null) { + return true; + } + if (idValue instanceof Integer) { + return idValue.equals(0); + } + if (idValue instanceof Long) { + return idValue.equals(0L); + } + if (idValue instanceof String) { + return StringUtils.isEmpty((CharSequence) idValue); + } + return false; + } } diff --git a/orm/src/main/java/com/zfoo/orm/model/persister/PNode.java b/orm/src/main/java/com/zfoo/orm/model/persister/PNode.java index 23615c18..243997ff 100644 --- a/orm/src/main/java/com/zfoo/orm/model/persister/PNode.java +++ b/orm/src/main/java/com/zfoo/orm/model/persister/PNode.java @@ -31,15 +31,21 @@ public class PNode> { // 修改数据的时间 private volatile long modifiedTime; - private volatile E entity; + // 记录最初访问时的线程信息 + private long threadId; + private String threadName; + public PNode(E entity) { this.entity = entity; var currentTime = TimeUtils.now(); this.writeToDbTime = currentTime; this.modifiedTime = currentTime; + + this.threadId = Thread.currentThread().getId(); + this.threadName = Thread.currentThread().getName(); } public E getEntity() { @@ -65,4 +71,20 @@ public class PNode> { public void setModifiedTime(long modifiedTime) { this.modifiedTime = modifiedTime; } + + public long getThreadId() { + return threadId; + } + + public void setThreadId(long threadId) { + this.threadId = threadId; + } + + public String getThreadName() { + return threadName; + } + + public void setThreadName(String threadName) { + this.threadName = threadName; + } }